Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: How to access derived class field values?

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to access derived class field values?

    Hi there.
    I want to access derived class field values. I can access field names with my code but for the values Its necessary to create a class instance.
    I don't know how to do. look at my code:
    for (Field f : getClass().getDeclaredFields()){
    	fieldNames += f.getName()+",";
    }
    How to access values?


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How to access derived class field values?

    I have no idea what you are asking. Please elaborate and maybe provide some more code or create a SSCCE.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to access derived class field values?

    Quote Originally Posted by Junky View Post
    I have no idea what you are asking. Please elaborate and maybe provide some more code or create a SSCCE.
    public class a{
      // here I want to read the value of name or f1 or f2 or every other field values from derived classes
    }
    public class b extends a{
      public string name;
    }
    public class c extends a{
      public string f1;
      public string f2;
    }

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How to access derived class field values?

    No! That is absolutely the wrong way to go about it. A parent class should be stand alone. By that I mean that if the child class did not exist then the parent class should be able to compile and execute. If you think you need the parent class to have access to instances of the child class then you have a bad design.
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to access derived class field values?

    Quote Originally Posted by Junky View Post
    No! That is absolutely the wrong way to go about it. A parent class should be stand alone. By that I mean that if the child class did not exist then the parent class should be able to compile and execute. If you think you need the parent class to have access to instances of the child class then you have a bad design.
    No man! it's the best idea which I've implemented in dot net.
    In dot net I can do this, I just want to know how to do it in Java.

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How to access derived class field values?

    You ask for advice and when you don't hear what you want you just ignore it!
    Improving the world one idiot at a time!

  7. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to access derived class field values?

    Quote Originally Posted by Junky View Post
    You ask for advice and when you don't hear what you want you just ignore it!
    I don't need advice, I just need solution!

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to access derived class field values?

    I don't need advice, I just need solution!
    That's one of the better quotes I've heard! Thanks for brightening my day, even though it may have left me a tiny bit sadder (but just for you).

  9. The Following User Says Thank You to GregBrannon For This Useful Post:

    Kewish (October 2nd, 2013)

  10. #9
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: How to access derived class field values?

    Quote Originally Posted by GregBrannon View Post
    That's one of the better quotes I've heard! Thanks for brightening my day, even though it may have left me a tiny bit sadder (but just for you).
    Come on, cheer up! The OP also uses a hammer to screw

  11. The Following User Says Thank You to PhHein For This Useful Post:

    Kewish (October 2nd, 2013)

Similar Threads

  1. append to text field declared out of class
    By chopficaro in forum Java Theory & Questions
    Replies: 1
    Last Post: July 25th, 2012, 03:23 PM
  2. scope problem: append to text field declared out of class
    By chopficaro in forum Java Theory & Questions
    Replies: 2
    Last Post: July 14th, 2012, 03:21 PM
  3. update static field and call method within the class?
    By GeneralPihota in forum Object Oriented Programming
    Replies: 7
    Last Post: February 6th, 2012, 09:20 PM
  4. problem with data access when a class call another class
    By ea09530 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2010, 05:20 PM