Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default A question about objects

    I want to use a set of objects in various different classes. I don't know how to do this. Do I really have to instantiate all the objects in every class in which I want to use them? This seems like a lot of redundancy.

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    "redundancy" is a big word isn't it...

  3. #3
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    Errr does that mean I do have to instantiate all the objects in each class that I use them?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You'll instantiate the objects that need to be instantiated. I'm not sure if there is any more specific answer for this somewhat general question.

  5. #5
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    I was trying to avoid posting a whole heap of code to ask a fundamental question. I'll try again:

    Suppose I have 3 classes A, B & C. I instantiate a bunch of objects in class A. I then want to use those objects in class B & C. Do I have to instantiate those objects again in classes B & C.

    If this isn't clear I'll post all my code and ask a specific question. Thanks

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I don't think that your question is answerable as currently asked. I don't think we need to see all of the code, but we do need enough information to make the question an answerable one. Asking questions in a forum is as much an art as coding is. You need to provide enough information so that the question is answerable, and yet you don't want to overload the volunteer with too much information -- so much that folks avoid the question. Please read the link on how to ask questions in my signature. It contains much useful advice in this regards. Much luck!

  7. #7
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    Quote Originally Posted by TaxpayersMoney View Post
    I was trying to avoid posting a whole heap of code to ask a fundamental question. I'll try again:

    Suppose I have 3 classes A, B & C. I instantiate a bunch of objects in class A. I then want to use those objects in class B & C. Do I have to instantiate those objects again in classes B & C.

    If this isn't clear I'll post all my code and ask a specific question. Thanks
    try posting the code if you dont mind.

  8. #8
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by TaxpayersMoney View Post
    I want to use a set of objects in various different classes. I don't know how to do this. Do I really have to instantiate all the objects in every class in which I want to use them? This seems like a lot of redundancy.

    what do you mean with "I want to use them"? if you only need some methods in the object you could declare these methods in the object as static, so you don't need an instance to use them. if not, you could create an collection of type objects and then passing the collection to the constructors of the other classes. i'm only guessing, better if you post your classes.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by TaxpayersMoney View Post
    I was trying to avoid posting a whole heap of code to ask a fundamental question. I'll try again:

    Suppose I have 3 classes A, B & C. I instantiate a bunch of objects in class A. I then want to use those objects in class B & C. Do I have to instantiate those objects again in classes B & C.

    If this isn't clear I'll post all my code and ask a specific question. Thanks
    An object of class A that instantiates those other objects can also hand them over to other interested parties; it could have a method:

    Java Code:
    public List<Object> getObjects() { ... }
    Objects of class B or class C have to know about that object of class A (the owner of the other objects) and ask it to hand the other objects over.

    kind regards,

    Jos

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Also see reply #7 in this thread

  11. #11
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    Oh dear. It seems that my ignorance has led me to believe that my question was a rather simple one, when in fact it isn't. After having read some of your replies I realise that I don't understand OOP enough to even ask what I'm doing wrong! Anyway, thanks for your attempts at help. I shall go away and try to learn more about OOP and come back and ask my question when I can be sure that it makes sense.

  12. #12
    PaperBeetle is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default

    An analogy (drawn from my own parochial existence :) )... Suppose I have a flat and my neighbour has a flat. My neighbour owns a mop, and I want to use that mop to clean my floors. Do I have to get my own mop?
    No - then I would be using my own mop, not my neighbour's. If I want to use my neighbour's mop, then I should borrow it from them. In programming terms, what JosAH said.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default

    Nice description...:)

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    Nice description...:)
    No it isn't: I hate cleaning floors.

    kind regards,

    Jos ;-)

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It would be far far better to get the neighbor to come over with their mop and clean your floor -- extension by composition (the mop comes with the neighbor -- is an object contained by the neighbor).

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fubarable View Post
    It would be far far better to get the neighbor to come over with their mop and clean your floor -- extension by composition (the mop comes with the neighbor -- is an object contained by the neighbor).
    Do you mind if I borrow your neighbours? You can have mine because they never come over to my place with their mop to clean the floor. All they do is drink my beer and liquor ... stupid neighbours, never even heard of composition ...

    kind regards,

    Jos ;-)

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    It would be far far better to get the neighbor to come over with their mop and clean your floor -- extension by composition (the mop comes with the neighbor -- is an object contained by the neighbor).
    Maybe be better for you, but I'm not convinced your neighbour would agree.

    It's not really their responsibility to mop your floor after all...

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    It's not really their responsibility to mop your floor after all...
    It's their moral obligation.

    kind regards,

    Jos ;-)

  19. #19
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    i had the time to implement the mob request. improvements are welcome

    Java Code:
    public class LetsClean {
    
    	public static void main(String[] args) {
    		Mop m = new Mop();
    		Neighbour n1 = new Neighbour("neighbour 1");
    		// neighbour 1 has a mob, so set it
    		n1.setMop(m);
    		Neighbour n2 = new Neighbour("neighbour 2");
    		// neighbour 2 has no mob but his neighbour 1, so set
    		// not the mob, but the neighbour 1
    		n2.setMopNeighbour(n1);
    		// n2 have no mob but call n1 and his mob
    		System.out.println(n2.getMopNeighbour().getMop());
    
    	}
    }
    
    class Mop {
    	String mob = "Here i'm, the mob";
    
    	public String getMob() {
    		return mob;
    	}
    }
    
    class Neighbour {
    	Mop mop;
    	String name;
    	Neighbour mopNeighbour;
    
    	public Neighbour(String n) {
    		name = n;
    	}
    
    	public void setMop(Mop m) {
    		mop = m;
    	}
    
    	public void setMopNeighbour(Neighbour n) {
    		mopNeighbour = n;
    	}
    
    	public Neighbour getMopNeighbour() {
    		return mopNeighbour;
    	}
    
    	public String getMop() {
    		return mop.getMob();
    	}
    }

  20. #20
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    I assume you have typos with Mop and Mob rather than that your code is missing a Mob class?
    With that assumption I wouldn't have a getMop method inside the Mop class. Rather a clean method which perhaps returns boolean. The clean method could take a Cleanable as an argument so we can pass it different things to clean.

    A Neighbour would also have a clean method so that calling aNeighbour.clean() would result in the aNeighbour calling his mop.clean method. That would represent asking my Neighbour to do the cleaning. IF I pass my room as the Cleanable object then I'm asking him to clean my room.
    If I wanted to do the cleaning myself using my Neighbour's Mop, then I would instead
    call aNeighbour.getMop().clean(myRoom).

Page 1 of 2 12 LastLast

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 09:49 AM
  2. initiating objects question(kinda)
    By helpisontheway in forum New To Java
    Replies: 4
    Last Post: 01-11-2010, 04:07 AM
  3. Simple question about objects.
    By shroomiin in forum New To Java
    Replies: 10
    Last Post: 10-10-2009, 03:33 AM
  4. Replies: 1
    Last Post: 01-22-2009, 05:25 PM
  5. Creating objects question
    By sergm in forum New To Java
    Replies: 2
    Last Post: 12-27-2007, 05:10 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
  •