Results 1 to 4 of 4
Thread: Passing objects to classes
- 11-30-2010, 05:01 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
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
And I want to have another class which contains two squares. So something 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; } }
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:class pair { public pair(Square s, Square s2) { } }
c in pair would update to 7 as well. Is there anyway of doing this? Thanks.Java Code:Square b = new Square(); Square c = new Square(); pair p = new pair(b,c); c.setNumber(7);
- 11-30-2010, 05:02 PM #2
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.
- 11-30-2010, 05:17 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
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:
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 programJava Code:class pair { public pair(Square s, Square s2) { } public Square getSquare() { return s; } }
- 11-30-2010, 05:27 PM #4
Similar Threads
-
Passing array objects
By drymsza1234 in forum New To JavaReplies: 1Last Post: 12-03-2009, 02:40 PM -
Passing Objects
By Java_Developer in forum New To JavaReplies: 6Last Post: 10-29-2009, 01:32 PM -
MVC passing objects
By simo_mon in forum New To JavaReplies: 1Last Post: 07-17-2009, 06:57 AM -
Passing objects into constructors
By aaronfsimons in forum New To JavaReplies: 8Last Post: 04-14-2009, 12:08 PM -
help with passing objects between classes
By aruna1 in forum New To JavaReplies: 7Last Post: 03-22-2009, 02:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks