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: Object as a Reference into Object's Class

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

    Post Object as a Reference into Object's Class

    Basically I have:

    A main class:
    public class mainClass 
    {
        public static void main(String[] args) 
        {
            extraClass First=new extraClass("First",1,true);
            extraClass Second=new extraClass("Second",2,true);
            extraClass Third=new extraClass("Third",3,false);
     
            extraClass.compareObjects(First,Second);
        }
    }

    And an extraClass:
    public class extraClass
    {
    	private String objectName;
    	private int objectID;
    	private Boolean isUsed;
     
    	public extraClass(String tempName,int tempID,Boolean tempUsed)
    	{
    		objectName=tempName;
    		objectID=tempID;
    		isUsed=tempUsed;
    	}
     
    	public static void compareObjects(extraClass objectOne,extraClass objectTwo)
    	{
    		if (((objectOne.objectName.equalsIgnoreCase("First")&&(objectTwo.objectName.equalsIgnoreCase("Second"))) || (objectOne.objectName.equalsIgnoreCase("Second"))&&(objectTwo.objectName.equalsIgnoreCase("First"))) && (!Third.checkUsed))
    		{
    			Third.setUsed(Third);
    			System.out.println("Yeah");
    		}
    	}
     
    	public Boolean checkUsed(extraClass tempName)
    	{
    		if (tempName.isUsed)
    			return true;
    		else
    			return false;
    	}
     
    	public void setUsed(extraClass tempName)
    	{
    		tempName.isUsed=true;
    	}
    }


    I'm given the errors:
    cannot find symbol variable Third
    cannot find symbol variable Third
    cannot find symbol variable Third

    The problem is in the compareObjects method in the extraClass when I attempt to use an object I created in the mainClass using the extraClass.
    Any ideas?
    Last edited by Ace Coder; November 28th, 2010 at 02:54 PM.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Object as a Reference into Object's Class

    && (!Third.checkUsed))
    Third.setUsed(Third);

    You invoke methods on Third, which hasn't been declared anywhere in your 'extraClass' Class.
    Not sure what you're trying to do with 'Third' in extraClass. I'm no expert by a long shot, but I got a feeling what you're thinking of doing isn't possible.

    Also it would be a good idea to stick to the convention of starting class names with capital letters and variables with lower case, so for your example, "ExtraClass" and variables as follow's "aVariable"

    Either way, good luck.

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

    Default Re: Object as a Reference into Object's Class

    Quote Originally Posted by newbie View Post
    && (!Third.checkUsed))
    Third.setUsed(Third);

    You invoke methods on Third, which hasn't been declared anywhere in your 'extraClass' Class.
    Not sure what you're trying to do with 'Third' in extraClass. I'm no expert by a long shot, but I got a feeling what you're thinking of doing isn't possible.

    Also it would be a good idea to stick to the convention of starting class names with capital letters and variables with lower case, so for your example, "ExtraClass" and variables as follow's "aVariable"

    Either way, good luck.
    The Third object was declared and initialized in the Main class.

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Object as a Reference into Object's Class

    Well yeah, but it has no reference within "extraClass", its an instance of extraClass.

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

    Default Re: Object as a Reference into Object's Class

    Quote Originally Posted by newbie View Post
    Well yeah, but it has no reference within "extraClass", its an instance of extraClass.
    Alright that's where I'm lost. How can I either get extraClass to 'see' Third, or possibly accomplish the same goal in a different way?

  6. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Object as a Reference into Object's Class

    Well firstly, state you're design specification, what are you trying to accomplish?

    Also is there a reason you are trying invoke a Boolean variable isUsed? i.e, its not a method.

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

    Default Re: Object as a Reference into Object's Class

    This is a small part of a project I've started on, but what I'm trying to accomplish with this example code is combine different objects to create a new object. This will be used in a simple DOS era alchemy type of game.

Similar Threads

  1. Object or Class?
    By mortalc in forum Object Oriented Programming
    Replies: 23
    Last Post: August 20th, 2010, 01:17 AM
  2. How to create a new object of another class within a method
    By davie in forum Object Oriented Programming
    Replies: 1
    Last Post: April 16th, 2010, 05:53 PM
  3. Object as Reference not working
    By jassi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 9th, 2010, 08:47 AM
  4. Class and Object
    By jyothishey in forum Object Oriented Programming
    Replies: 2
    Last Post: January 25th, 2010, 07:48 AM
  5. Replies: 2
    Last Post: November 5th, 2009, 09:15 PM