DNB5brims
DNB5brims

Reputation: 30578

What is the best practise for java to let call back the class?

Here is the problem:

I got a class called MyClass, which is a normal class, have a variable i, and MyOtherClass will have some logic inside to modify MyClass's i value, but I don't want to pass the i value to MyOtherClass, I would like them to call addOne, and minusOne method. How can I modify the MyOtherClass to do so? I can modify the constructor of MyOtherClass to fulfil this requirement. Thanks.

 public class MyClass{

   int i;

   public void myMethod(){

     //I would like to myOtherClass a, b can call back MyClass's addOne, and minusOne
     MyOtherClass a = new MyOtherClass(); 
     MyOtherClass b = new MyOtherClass();

     a.runLogic(); 
     b.runLogin();   
   }

   public void addOne(){
     i++;   
   }

   public void minusOne(){
     i--;   
   }  

   pubic void printI(){
    System.out.print(i);
   }
}

The code that use MyClass

MyClass myClass = new MyClass(); 

myClass.myMethod(); 

myClass.printI();

Upvotes: 0

Views: 60

Answers (4)

William Morrison
William Morrison

Reputation: 11006

Pass a reference to MyClass into MyOtherClass, and call the methods you need directly.

If you're looking for a more generic solution, make MyClass implement some interface and accept an instance of that interface in MyOtherClass instead. This would allow more flexibility.

//here's your interface.
public interface IMyClass{
    void addOne();
    void minusOne();
}

//implement interface in MyClass.
public class MyClass implements IMyClass{/*...*/}

public class MyOtherClass{
    IMyClass myClass;
    public MyOtherClass(IMyClass myClass){
        this.myClass = myClass;
    }

    public void test(){
        myClass.addOne();
        myClass.minusOne();
    }
}

This is appropriate for more complex types than this. Here its a bit silly because the class is doing so little, but maybe your example is simplified for something more complicated. Just advice.

Upvotes: 0

Rakesh Soni
Rakesh Soni

Reputation: 10897

Pass the object of MyClass into the MyOtherClass constructor like

Class MyOtherClass 
{
MyClass mc;

// constructor
MyOtherClass (MyClass mc)
{
this.mc = mc;
}

void testMethod(){
  mc.i = 10;        // or whatever you want to assign
}

}

Upvotes: 0

M Sach
M Sach

Reputation: 34424

you just need to pass current object of MyClass to My OtherClass. You can do this by

MyOtherClass a = new MyOtherClass(this); 
MyOtherClass b = new MyOtherClass(this);

Now inside your MyOtherClass you need to modify your constructor

public MyOtherClass(MyClass myClass) {
        this.myClass = myClass;
    }

Now you can call any method of myClass from MyOtherClass

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691755

Simply pass this as argument to the other class constructor or runLogic method:

public class MyClass {
    private int i;

    pulic void addOne() {
        i++;
    }

    public void myMethod() {
        MyOtherClass c = new MyOtherClass(this);
        c.runLogic();
    }   
}

public class MyOtherClass {
    private MyClass myClass;

    public MyOtherClass(MyClass myClass) {
        this.myClass = myClass;
    }

    public void runLogic() {
        myClass.addOne();
    }
}

Upvotes: 5

Related Questions