?
Solved

inform calling object

Posted on 2002-04-04
4
Medium Priority
?
239 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 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month16 days, 10 hours left to enroll

864 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