Results 1 to 10 of 10
  1. #1
    ehsansh is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default How to access derived class field values?

    Hi there.
    I want to access to a derived class fields.
    I'm using this code:
    Java Code:
    		for (Field f : getClass().getDeclaredFields()){
    			fieldNames += f.getName()+"="+f.get();
    		}
    but f.get wants an instance of the class.
    what should I do?
    Is there any better way to do this?

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,895
    Rep Power
    5

    Default Re: How to access derived class field values?

    Without an object instance, what would you expect f.get() to actually return? The value of a static field by any chance?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    ehsansh is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default Re: How to access derived class field values?

    Thanks for your reply gimbal,
    but how can I make an instance when I don't know the name of derived class?
    can you tell me how to define that class?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,524
    Rep Power
    5

    Default Re: How to access derived class field values?

    Well, I'm confused. You want to access a class you don't know the name of. Do you know what the methods do? How do you plan on using the class?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,895
    Rep Power
    5

    Default Re: How to access derived class field values?

    Quote Originally Posted by ehsansh View Post
    but how can I make an instance when I don't know the name of derived class?
    You are missing the point. When you do not have an instance of the class, you don't have any values to get. Only a living person has an actual name that you could get from the Person object; you can't get it from the Person class. You can only know that Person has a Field 'name'. So unless you have an actual instance of Person with the name 'Bill', there is no reason to ever want to call get().

    So I pass the question back to you: why do you think you should be calling get() without an object instance?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: How to access derived class field values?

    The Class class has a method to create a new instance. You can also enumerate the constructors and invoke them.

    But yes... why do you want to do this? There might be a better way.
    Get in the habit of using standard Java naming conventions!

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: How to access derived class field values?

    How to access derived class field values?

    OP has already been told this is a bad idea in their duplicate post.

  8. #8
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: How to access derived class field values?

    LOL... dot net.
    Get in the habit of using standard Java naming conventions!

  9. #9
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: How to access derived class field values?

    Like the rest of the guys here, I don't think I get your question... But here's what I would like to clarify with you:
    we've got two classes Parent and Child
    class Parent{ //all sorts of things}
    classs Child extends Parent{//all sorts of things}

    you want class Parent to access class Child? that's not possible... because class Parent doesn't even know if class Child exists
    Now I asssume what you were trying to say is something like this

    Parent pc = new Child(); // A reference variable of type Parent pointing to a Child object
    but now you know that this particular object is of type Child... and the Child has some attributes that you would like to access
    then you'd have to downcast
    ((Child)pc).getName(); or Chlid realKid = (Child)pc;

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,895
    Rep Power
    5

    Default Re: How to access derived class field values?

    Quote Originally Posted by kjkrum View Post
    LOL... dot net.
    Apparently using dot net you can do amazingly impossible and illogical things.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Passing instance of derived class as base class
    By goatjugsoup in forum New To Java
    Replies: 7
    Last Post: 08-01-2012, 05:15 AM
  2. How to get Integer values from Text Field
    By aks_saxena in forum AWT / Swing
    Replies: 3
    Last Post: 04-09-2011, 01:00 PM
  3. Replies: 4
    Last Post: 01-17-2010, 11:13 PM
  4. Entity - Field-Based Access Vs Property Based Access
    By CatchSandeepVaid in forum Enterprise JavaBeans (EJB)
    Replies: 3
    Last Post: 11-02-2009, 07:18 PM
  5. Method access or field access
    By carderne in forum New To Java
    Replies: 2
    Last Post: 12-06-2008, 06:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •