Results 1 to 4 of 4
  1. #1
    skybluenova is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Modifying an object's attributes by using another object's method

    Hello all, I have a question that most of you will probably laugh about. But for my own purposes I am currently learning Java, and as I am quite new to programming and OOP, this problem actually arouse:

    Given, I have two objects: Controller c1 and Bulb b1. This Controller object has a method called "setBulb", and it should be setting a "Bulb"'s attribute "isOn" to either true or false.

    How do I accomplish that? I can't do this in the "Controller"'s method, as the object b1 has not yet been created and is therefore unknown ...

    I hope this is not totally nonsense. I'd really appreciate any suggestions, and if what I want to do is totally bad programming style, I am glad if you'd correct me.

    Best Regards
    Dennis

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: Modifying an object's attributes by using another object's method

    If a Controller needs to set a Bulb to on or off, it needs to have a Bulb; it can either create one or you can 'give' it one, otherwise the Controller doesn't have a Bulb and can't do anything ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    skybluenova is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Re: Modifying an object's attributes by using another object's method

    Thanks for the swift reply, Jos. So I thought, but lets say there are about 30 Bulb objects that need to be handed over ... how do I put them in an array?

    Best
    Dennis

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: Modifying an object's attributes by using another object's method

    Quote Originally Posted by skybluenova View Post
    Thanks for the swift reply, Jos. So I thought, but lets say there are about 30 Bulb objects that need to be handed over ... how do I put them in an array?
    One by one? Better put them in an ArrayList<Bulb>; it anticpates for more or less Bulbs to be put in it. Possibly a Bulb can put itself in the ArrayList<Bulb> in it's constructor that accepts the ArrayList<Bulb> as a parameter ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 02-02-2011, 07:11 PM
  2. Replies: 2
    Last Post: 06-22-2010, 05:29 AM
  3. Replies: 2
    Last Post: 01-29-2010, 03:01 AM
  4. Replies: 1
    Last Post: 03-04-2009, 07:14 PM
  5. Classes with object attributes
    By RRasco in forum New To Java
    Replies: 5
    Last Post: 11-25-2008, 01:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •