Private field access through base class

  • Thread starter =?iso-8859-1?B?bW9vcJk=?=
  • Start date
?

=?iso-8859-1?B?bW9vcJk=?=

Hi,
I build a base class to read and write private fields of sub-classes,
but bursts IllegalAccessException.

public class BaseClass{
public void read(){
//reflection code

}
public void write(){
// also reflection code
}
}

public class SubClass extends BaseClass{
private String name;
//other methods
}

How can I access the private fields, such as name in SubClass inside
BaseClass?
 
F

fsa71

If I understand your question, I think you should create get/set
methods in your BaseClass to access this fields.
 
C

Chris Uppal

moopT said:
I build a base class to read and write private fields of sub-classes,
but bursts IllegalAccessException.

public class BaseClass{
public void read(){
//reflection code

Why are you using reflection to access nested classes ? There can't be any
need for it that I can imagine.

If you are not using reflection to do so, then I think you must be doing
something wrong (that is not visible in your example) since an outer class does
have access to nested classes private members.

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top