Solved

inform calling object

Posted on 2002-04-04
4
213 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
  • 3
4 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 50 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 191
Should localization be done inside spring controller 5 25
tomcat not starting 6 45
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 31
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now