Results 1 to 6 of 6
Like Tree3Likes
  • 1 Post By Fubarable
  • 1 Post By j2me64
  • 1 Post By Norm

Thread: Accessing an object from within another object from the same type in Java

  1. #1
    mhasgari is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default Accessing an object from within another object from the same type in Java

    Suppose we wrote a class, named Class1 and instantiated two objects from that, named MyClass1 and MyClass2. Now we must invoke one of MyClass2's methods in MyClass1, if some conditional statement is true. How is it possible? In other words how can we access MyClass2 from within MyClass1?
    Thanks

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

    Default

    Quote Originally Posted by mhasgari View Post
    Suppose we wrote a class, named Class1 and instantiated two objects from that, named MyClass1 and MyClass2. Now we must invoke one of MyClass2's methods in MyClass1, if some conditional statement is true. How is it possible? In other words how can we access MyClass2 from within MyClass1?
    Thanks
    By holding a reference of one object inside of another and calling the method on the held reference.

    Sorry, but I find your question somewhat vague. If you need more specific help, please post more detailed information about your problem.
    mhasgari likes this.

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

    Default

    an other possiblity instead of using references is declaring the methods in the class as static.
    mhasgari likes this.

  4. #4
    mhasgari is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks, but I think I should add more details for my case.
    I am simulating a simple message passing system in Java, in order to analyze some checkpointing algorithms. In it, there are bunch of Processes, which pass messages to each other, and according to the contents of some of these messages, the receiver process should do some distinct operation, like adding some data to a data structure, taking a checkpoint, etc. So, I wrote a class named Process, and added a method, named Receive, to it, because I think it is a suitable way to implement this message passing thing. On the other hand a process should send a message to some of other processes (and not all of them) if a particular condition statement is true.
    I think I should instantiate 10 (or other distinct number of) objects, in my main class, and its main method, and assign each one of them to a thread, so they can run simultaneously and interact each other according to a statistical distribution. But the question is how an object from the class Process, can access to the receive method of another particular one, in order to pass a message to it?
    Please let me know if you think the OO design of my project is not perfect, and if so, which design will be the best.
    Thanks

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    how an object from the class Process, can access to the receive method of another particular one, in order to pass a message to it
    Put all the Process objects in a container with a manager that will send the message to all the objects in the container except for the message's sender.
    mhasgari likes this.

  6. #6
    mhasgari is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Put all the Process objects in a container with a manager that will send the message to all the objects in the container except for the message's sender.
    It sounds Perfect, Thanks.

Similar Threads

  1. Replies: 2
    Last Post: 06-01-2011, 09:44 AM
  2. Accessing GUI object methods
    By km0r3 in forum AWT / Swing
    Replies: 5
    Last Post: 10-06-2010, 01:39 AM
  3. Wrap an object into another object type
    By einnhann in forum New To Java
    Replies: 7
    Last Post: 01-09-2010, 04:35 PM
  4. using instanceof to get Object type and parent type?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-06-2008, 06:24 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 03:33 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
  •