?
Solved

inform calling object

Posted on 2002-04-04
4
Medium Priority
?
234 Views
Last Modified: 2010-03-31
Very rough example set up...

class A {
.
.
  B b = new B(123);
  // Add a listener here? so that when b.myID alters something happens.
.
.
}

public class B{
  int myID = 0;

  public B(setB){
    myID = setB;
  }

  public void setB(){
    myID = 312;
  }
}

How do I code so that when the value of myID changes in the instance of B then a method is called in the generating object of class A?

I'm guessing I'll need to add a listener of some kind to class A and a method to handle this in B???

Cheers,

Mal
0
Comment
Question by:malcx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 200 total points
ID: 6918353
There are lots of possibilities.

The straightforward one simply takes advantage of the use of the settor method. Since, presumably myID can only be changed by a call to setB, that method could call A to notify it of the change. B would need a reference to A to accomplish that of course. Something like:

class A {
.
.
 B b = new B(this,123);

public void bChange(int m){
   //do whatever is necessary here
}

.
.
}

public class B{
 int myID = 0;
 A ana;

 public B(A ta,setB){
   myID = setB;
   ana=ta;
 }

 public void setB(){
   myID = 312;
   ana.bChange(myID);
 }
}

Alternatively you could use a more generic mechanism like the Observer/Observable facilities. These would require that B be derived from Observable, and that A implements the Observer interface. B would then, in the setB method, make a call to notifyObservers which would call the update method in any registered observers. Something like:

class A implements Observer {
.
.
 B b = new B(this,123);
 b.addObserver(this);

public void update(B ab,Object a){
   //do whatever is necessary here
}

.
.
}

public class B extends Observable {
 int myID = 0;
 A ana;

 public B(A ta,setB){
   myID = setB;
   ana=ta;
 }

 public void setB(){
   myID = 312;
   notifyObservers();
 }
}

In this form it would require that the myID variable be inspectable from an A object. Alternatively you could get the value to it in an object wrapper by using the alternate form of notifyObservers, through which you can pass an object.

You could undoubtedly also cast this in the form of a Listener interface. On the other hand, Listeners as such were really designed for communicating system events. For the task at hand, it would probably come down to something very similar to an Observer/Observable pair.

0
 
LVL 3

Author Comment

by:malcx
ID: 6918438
Great thanks!
Last time I programmed in Java I basically used it procedurally. I'm just undertaking another large project (after about 18 months spent with other languages...) and knew this'd be fairly simple.

Thanks again,

Mal
0
 
LVL 3

Author Comment

by:malcx
ID: 6918535
Maybe I was a little hasty...
The code I'm currently working with is creating an instance of B from main() and as such won't let me use the non-static "this"...
???
Mal
0
 
LVL 3

Author Comment

by:malcx
ID: 6920385
OK have fixed now,cheers
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question