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 7 of 7

Thread: [Question] Objects instantiated within objects.

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Location
    Chicago, IL
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question [Question] Objects instantiated within objects.

    Been a while since I used Java and was wondering if this was a decent or even correct way of setting this up.

    FYI, userResults refers to a JDBI variable that isn't present in the code below.

    Feel free to suggest a better method, thanks.
    public class Stat()
    {
    	private int current;
    	private int max;
     
    	public int getCurrent()  {return current;}
    	public void setCurrent(int current)   {this.current = current;}
     
    	public int getMax()  {return max;}
    	public void setMax(int max)   {this.max = max;}
    }
     
     
    public class Character()
    {
    	Stat hp = new Stat();
    	Stat mp = new Stat();
    }
     
    Character thisCharacter = new Character();
     
    thisCharacter.hp.setCurrent((Integer) userResults.get("hpColumn1"));
    thisCharacter.hp.setMax((Integer) userResults.get("hpColumn2"));
    Last edited by Xerosigma; April 25th, 2012 at 01:34 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,442
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default Re: [Question] Objects instantiated within objects.

    One problem I see is with the classname: Character is a Java SE class.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Location
    Chicago, IL
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: [Question] Objects instantiated within objects.

    Quote Originally Posted by Norm View Post
    One problem I see is with the classname: Character is a Java SE class.
    Ah, I'll consider changing it then. Other than that, would there be a better way to do this? Thanks.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,442
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default Re: [Question] Objects instantiated within objects.

    way to do this
    Do what? Can you explain what you are trying to do?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Location
    Chicago, IL
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: [Question] Objects instantiated within objects.

    Quote Originally Posted by Norm View Post
    Do what? Can you explain what you are trying to do?
    Set up a family of objects, or object trees. Don't know what the proper terminology is in Java. But basically have the final object of Character which is composed of other objects, like... Heart, Brain, etc. And of course, each of those constituents may be composed of their own objects as well.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,442
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default Re: [Question] Objects instantiated within objects.

    The class has two member variables that reference Stat class objects. About as simple a class as you can make.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    Xerosigma (April 25th, 2012)

  8. #7
    Junior Member
    Join Date
    Apr 2012
    Location
    Chicago, IL
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: [Question] Objects instantiated within objects.

    Thank you. Just wanted to get that clarified.

Similar Threads

  1. WORKIN WIT 3D OBJECTS
    By susheela in forum Java Theory & Questions
    Replies: 3
    Last Post: December 12th, 2011, 07:20 AM
  2. Question Dynamically create objects, set value and call
    By buntyindia in forum Java Theory & Questions
    Replies: 1
    Last Post: May 25th, 2011, 07:50 AM
  3. java question on objects
    By joe98 in forum Threads
    Replies: 2
    Last Post: April 12th, 2011, 03:54 PM
  4. New to Objects...
    By Java Neil in forum What's Wrong With My Code?
    Replies: 17
    Last Post: March 24th, 2011, 07:00 AM
  5. Objects
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: January 20th, 2010, 11:50 AM