-6

I have a simple code below

class Test{

  public static Test create(){
    return this;// this is non-static context
  }

  public static void main (String[] args){
    Test ob = Test.create();
  }
}

How can I create object like this? If I remove static then not able to call Test.create() method. actually it is related to

Toast toast = Toast.makeText(Context,CharSeqText,int);
halfer
  • 19,824
  • 17
  • 99
  • 186
Avinash Kumar
  • 288
  • 6
  • 21

2 Answers2

1

You would use a Static Factory

public class Test{

  private Test() {
    //Prevent construction by other classes, forcing the 
    // use of the factory
  }

  public static Test create(){
    return new Test();
  }

  public static void main (String[] args){
    Test ob = Test.create();
  }
}

I've made a few changes to your example to show that this way an instance can only be obtained via the factory. This gives you control over how (and how many) instances are created.

For instance, you can also make it such that the factory always returns the same instance (a singleton)

private static Test instance;
public static Test create(){
   synchronized (Test.class) {
      if(instance == null)
         instance = new Test();
   }

   return instance;
}

This way the class Test would only get created a single time.

There are many other variations you can build into your factory method based on your specific requirements.

pfranza
  • 3,292
  • 2
  • 21
  • 34
  • This is a bad implementation of a singleton. – anemomylos Aug 11 '17 at 20:37
  • Agree, but it is the 'Classic Singleton' and does demonstrate the versatility of the static factory. – pfranza Aug 11 '17 at 20:38
  • Is erroneous the implementation you post. Method `create()` can run concurrently and return two different `instance` objects. – anemomylos Aug 11 '17 at 20:39
  • @easyjoin.net yes but the original question is dealing with the static factory pattern. For the sake of a clean example, I was not muddying up the code with sync block and thread locks. – pfranza Aug 11 '17 at 20:43
  • Please read this https://stackoverflow.com/questions/2094211/difference-between-singleton-and-factory-pattern. Your `singleton` implementation is wrong and the singleton pattern has nothing to do with the static factory. – anemomylos Aug 11 '17 at 20:46
  • You are missing the point, the answer is meant to illustrate the function of the static factory pattern, and I illustrated it both by itself and an example where it is used in conjunction with another pattern. – pfranza Aug 11 '17 at 20:53
0

You can return new Test.

public static Test create(){
    return new Test();
}

This is called a static factory method for object creation pattern. Item 1 of the Effective Java book by Joshua Bloch.

Bob
  • 13,447
  • 7
  • 35
  • 45
  • Actually, that depends a bit on `Test`. In the sample code shown in the question, yes, you could return a new `Test` instance. My guess is that the OP has something else in mind than this `Test` class (e.g., a `Context` in Android), and there are some classes where creating a new instance will not work properly (e.g., in Android, trying to create an `Activity` instance via `new`). – CommonsWare Aug 11 '17 at 20:34
  • 1
    Yes. I totally agree with you. From the question, I understood that he wants to create a Test object instance using a static method as we can create a Toast object using a static method `makeText`. – Bob Aug 11 '17 at 20:41
  • Yaah actually it is, I am new in Android.?? – Avinash Kumar Aug 11 '17 at 20:50