1

I have a class A in my Android app where I have some methods. Those methods are public and used in other classes (B, C, D, E, F ...).

Is there a possibility to create only once the object from the class A and then use it in the other classes or i have to create new object in each classe.

Actually I have to do in each classe (B, C, D, E, F ...)

A a = new A();
a.xxxx;

It will be great if I can create only once the object a and then call it in my other classes.

thank you.

ngesh
  • 13,398
  • 4
  • 44
  • 60
Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265

5 Answers5

6

Use a singleton pattern. It allows you to use the same instance across classes:

http://www.javabeginner.com/learn-java/java-singleton-design-pattern

soren.qvist
  • 7,376
  • 14
  • 62
  • 91
3
class A{
    static A a;
    static{
          a = new A();
    }
}

In every other class use

A.a to get the object and call respective methods as
A.a.xxxx()
Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
2

Use static methods's analogy to do this.

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();

Or Singleton pattern would be an alternate too.

waqaslam
  • 67,549
  • 16
  • 165
  • 178
1

Instead of that why don't you make those methods static or consider single instance pattern if there are no states involved..

how to use static methods and singleton pattern

ngesh
  • 13,398
  • 4
  • 44
  • 60
  • Thank you @sandy, can you please give me some more precisions about this? – Milos Cuculovic Apr 12 '12 at 12:20
  • static methods are generally discouraged as a bad idea for most purposes, as they make your design harder to change later. The singleton pattern is probably a better solution here. – Jules Apr 12 '12 at 12:25
  • @Jules.. If there are no states involved why not..? Easy to access.. and simple code... – ngesh Apr 12 '12 at 12:26
  • @Jules.. read this http://stackoverflow.com/questions/752758/is-using-a-lot-of-static-methods-a-bad-thing – ngesh Apr 12 '12 at 12:29
  • Thank you @sandy, I will try this. One more question, what if I need the context in my A class and this is not an activity? – Milos Cuculovic Apr 12 '12 at 12:50
  • If consider making it a Single Instance.. then You can take Context as its argument in **getInstane()** method.. or if you go with statics i think you need to add Context argument to all methods which needs it.. – ngesh Apr 12 '12 at 12:54
1

I see the following 3 possibilities:

1. If these are just "normal" helper methods you may also just do

class B extends A

and inherit the methods of A into B,C,D,E,....

2. However if you need internal memory of class A which is global to all other classes or their instances of B,C,D then may use the static pattern like

class A{

static int myGlobalIntVariable; //which is accessible from everywhere 
static void myHelperMethod1() {

}

or 3. Also you may use singleton as mentioned above which creates an instance that you use everywhere.

Just as a remark, you may use singleton or static pattern depending what you prefer when accessing the methods.

For static patter you have to call them like:

A.myHelperMethod();

and for singleton you have to do:

A.getSingleton().myHelperMethod1();  or A.singleton.myHelperMethod1()

if you have defined a variable called singleton within Class A

Which one to use really depends on your needs and taste :-)

user387184
  • 10,953
  • 12
  • 77
  • 147