Private field access through base class

Discussion in 'Java' started by =?iso-8859-1?B?bW9vcJk=?=, Nov 1, 2005.

  1. 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?
     
    =?iso-8859-1?B?bW9vcJk=?=, Nov 1, 2005
    #1
    1. Advertising

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

    Guest

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

    -------------------
    http://www.excelsior-usa.com/jet.html
    JVM based on Ahead-of-Time compilation
     
    , Nov 1, 2005
    #2
    1. Advertising

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

    Chris Uppal Guest

    moopT wrote:

    > 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
     
    Chris Uppal, Nov 1, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Banaticus  Bart
    Replies:
    9
    Views:
    616
    Gary Labowitz
    May 7, 2004
  2. DaveLessnau
    Replies:
    3
    Views:
    428
    Howard
    May 16, 2005
  3. Alf P. Steinbach
    Replies:
    6
    Views:
    549
    John Carson
    Sep 3, 2005
  4. Replies:
    2
    Views:
    816
    Noah Roberts
    Jun 9, 2006
  5. Replies:
    4
    Views:
    476
    Zeppe
    Sep 12, 2008
Loading...

Share This Page