Solved

inform calling object

Posted on 2002-04-04
4
224 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 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

726 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