-1

What is the difference between these two definitions?

public class MainClass {
    //definition one
    private static MainClass main = new MainClass();
    //definition two
    private static MainClass main2 = MainClass.getMain2();

    public static void main(String[] args) {

    }

    private static MainClass getMain2() {
        return main2;
    }

}

Which one is better?

NereplaN
  • 17
  • 1
  • 1
    You understand that if you don't use the `new` keyword, you aren't creating an object, right? So initializing `main2` to itself will just leave it as `null`? – azurefrog Sep 16 '19 at 15:16
  • 1
    Besides that non-descript title (please read [ask]) the initialization of `main` is better. Why? Because `main2` won't do what you expect... (just try it). – Thomas Sep 16 '19 at 15:16
  • Looks like you're getting confused between creating an instance of a class, and getting an instance from a singleton or something similar. Also the main() method is irrelevant, that's just your entrypoint. Did you mean to write a default contructor? – ewanc Sep 16 '19 at 15:19
  • first one is creating (and assigning) a new instance; 2nd is just assigning what is returned by a method (in that case the field being assigned to, so it is effectively a no-operation) – user85421 Sep 16 '19 at 15:21

1 Answers1

1

I think your code is wrong. This method should be in some other class. It should be like

private static MainClass getMain2() {
    return new MainClass();
}

It's called dependency injection.

Your code should be like below

public class MainClass {
    //definition one
    private static MainClass main = new MainClass();
    //definition two  
    private static MainClass main2 = MainClass.getMain2();

    public static void main(String[] args) {

    }

    //constructor injection
    private static MainClass( MainClass a) {
       this.main2  = a;
    }
    //Or in place of constructor injection you can use Setter injection
    public static setMain2( MainClass a) {
        this.main2  = main2;
    }

@Thiago Arrais answered on What is dependency injection? is quite easy to understand without serious coding/framework knowledge -

The best definition I've found so far is one by James Shore:

"Dependency Injection" is a 25-dollar term for a 5-cent concept.Dependency injection means giving an object its instance variables.

There is an article by Martin Fowler that may prove useful, too.

Dependency injection is basically providing the objects that an object needs (its dependencies) instead of having it construct them itself. It's a very useful technique for testing, since it allows dependencies to be mocked or stubbed out.

Dependencies can be injected into objects by many means (such as constructor injection or setter injection). One can even use specialized dependency injection frameworks (e.g. Spring) to do that, but they certainly aren't required. You don't need those frameworks to have dependency injection. Instantiating and passing objects (dependencies) explicitly is just as good an injection as injection by framework.

SSP
  • 2,703
  • 5
  • 27
  • 47