1

I have one class which is calling other classes, this class is my main. Is it possible to get somehow instance of this main class, that in others classes i could call main class methods ?

If yes so how this should be done in android with java?

Thanks.

Streetboy
  • 4,351
  • 12
  • 56
  • 101

2 Answers2

0

Use Singleton design pattern to achieve this.

Or if you want, you may use Static methods analogy for reusing the methods too.

For example:

public class Helper{
    public static void doSomething(){
        //do something here
    }
}

Now in your other classes, use the above method as below:

Helper.doSomething();
waqaslam
  • 67,549
  • 16
  • 165
  • 178
0

If you are creating the other classes in your main class, one approach would be to pass the current instance of your main class (i.e. `this') as a parameter to the constructors of the other classes, and have them store that reference for future use.

Alternately, you could simply pass the current instance of your main class as a parameter to the method you are calling, that needs to be able to call back.

JesusFreke
  • 19,784
  • 5
  • 65
  • 68