Results 1 to 4 of 4
  1. #1
    Catfish1 is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default Passing objects to classes

    Hi
    I'm having a problem trying to create a class which contains a group of another class I have made. Basically I have a class called square, which looks like this
    Java Code:
    class Square
    {
    	int number;
    
    	public Square()
    	{
    	
    	}
    
    	public void setNumber(int num)
    	{
    		if((num > 0) && (num <= 9))
    		{
    			set.clear();
    			set.add(num);
    			number = num;
    		}
    		else
    		{
    			System.out.println("You have entered an illegal number");
    		}
    	}
    
    	public int getNumber()
    	{
    		return number;
    	}
    }
    And I want to have another class which contains two squares. So something like this.

    Java Code:
    class pair
    {
    	public pair(Square s, Square s2)
    	{
    
    	}
    }
    However, I know this won't really do what I want it to do, as I need the Squares s and s2, to be updated when I update the original square in my main program. So if I did something like this in my main program:

    Java Code:
    Square b = new Square();
    Square c = new Square();
    
    pair p = new pair(b,c);
    
    c.setNumber(7);
    c in pair would update to 7 as well. Is there anyway of doing this? Thanks.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,037
    Rep Power
    10

    Default

    What happened when you tried exactly that? That should work just fine. If not, you should post an SSCCE demonstrating what you're actually doing.

  3. #3
    Catfish1 is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    Sorry, apologies I missed out the fact that I need to be able to interact with the variables in pair, using a get method or something similar to return the variables in it like:

    Java Code:
    class pair
    {
    	public pair(Square s, Square s2)
    	{
    
    	}
            public Square getSquare()
            {
                  return s;
            }
    }
    However if I do this, it can't find variable s, which I understand why it can't, as it is local, but if I create a Square within the class to get the value, it won't update if I update the square in the main program

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,037
    Rep Power
    10

    Default

    Then just keep track of the Square instances you pass in. Like this example:

    Java Code:
    public class Cat{
       String myName;
       public Cat(String name){
          myName = name;
       }
       public String getName(){
          return myName;
       }
    }

Similar Threads

  1. Passing array objects
    By drymsza1234 in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 03:40 PM
  2. Passing Objects
    By Java_Developer in forum New To Java
    Replies: 6
    Last Post: 10-29-2009, 02:32 PM
  3. MVC passing objects
    By simo_mon in forum New To Java
    Replies: 1
    Last Post: 07-17-2009, 07:57 AM
  4. Passing objects into constructors
    By aaronfsimons in forum New To Java
    Replies: 8
    Last Post: 04-14-2009, 01:08 PM
  5. help with passing objects between classes
    By aruna1 in forum New To Java
    Replies: 7
    Last Post: 03-22-2009, 03:41 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
  •