0

I've seen this code which has two different way its written

public static void main(String[] args) {
    Tester.boot();
    new tester();

Tester class:

public static tester instance;

public static void boot() {

    if (instance == null)
        instance = new tester();
}

public Tester() {
    System.out.println("test made");
}

What is the difference beetween tester.boot(); and new tester();

Are there any benefits to either?

Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
the133448
  • 75
  • 7

5 Answers5

2

There are three concepts being displayed here:

Tester class:

if (instance == null)
    instance = new tester();

That's used when you wish to control the construction of an object. The variable instance controls if new Tester() will be called by boot().

This can be used to implement Lazy Initialization. That's the practice of delaying construction of an object until it is needed. It's typically used when construction is both expensive and not always needed. Here it is delayed until boot() is called. Lazy Initialization is not only used for singletons. Some believe that using Lazy Initialization in java to create a singleton is actually wrong because of how the class loader works. They claim it's a habit ported over from c++ where it made sense. As a pattern, singleton has been controversial enough that some call it an anti pattern.

Tester class:

public static tester instance;

boot(), constructs a Tester object, sets instance to that object (Note: tester should have been capitalized). The fact that instance, which is of type Tester, resides in theTesterclass means thatTesteris [**self referential**][7], like the nodes in a linked list. But, unlike nodes, becauseinstanceisstatic, allTesterobjects have the same copy ofinstance`.

Because boot never puts what it constructs anywhere but in instance, only one boot() constructed Tester object ever exists at any one time, so long as instance is never copied anywhere. It easily could be, because it's public.

So long as instance is never set back to null boot() will construct a Tester only once no matter how many times it's called. Since instance is public it could be set to null at any time by anything that knows about Tester. Doing so would allow boot() to construct again and the old Tester object would be forgotten (garbage collected).

Since Testers constructors have not been made private there is no guaranty that Tester is never constructed by something other than boot(). That means there could still be many Tester objects running around. But they all have one instance in them that may or may not be themselves.

A classic singleton would have Lazy Initialization, be self referential, and be encapsulated in such a way that at most only one Tester object would ever exist. However, There is nothing here that proves this was meant to be a singleton in the first place. This could be a construction optimization gone horribly wrong.

Community
  • 1
  • 1
candied_orange
  • 7,036
  • 2
  • 28
  • 62
1

This is sort of implementation of a SINGLETON pattern, in Java. Look here to understand the concepts behind this pattern click here.

Shortly, you want to use this pattern if you want to have an instance of the class Instance trough all application's life cycle.

To understand it, the key in your code is in the part "if(instance == null)" {instance should be written with lower key, as it should be a class variable}.

If you go simply with the "new ..." any nuber of instances of this variables could be created. If instead you use the boot method, you are sure that the instance is always the same.

Anyway in the implementation you have written something is missing.

You should refer to this link, where there is also a complete implementation in Java programming language.

Gaetano Piazzolla
  • 1,388
  • 1
  • 16
  • 31
0

boot is useless, you're not showing how Instance is used. But I think it's there to implement a singleton, meaning to assure that only one instance of the class is created. I don't think it's a good implementation as Instance can be used by mistake without being initialized.

When you write new tester() then you're creating a new instance of tester object.

Please follow Java Naming Convention and rename your variables accordingly.

Maroun
  • 94,125
  • 30
  • 188
  • 241
0

To be specific--if you use tester.boot() it would set "instance" properly, if you call the constructor directly, it will not.

This is a really broken version of the singleton pattern, to do it correctly the constructor should be made private which makes new Tester() impossible.

Also, the boot method should look a little more like:

public synchronized static Tester  boot()
{
    if(instance == null) {
        instance=new Tester();
    }
    return instance;
}

and even that is probably iffy.

Maroun
  • 94,125
  • 30
  • 188
  • 241
Bill K
  • 62,186
  • 18
  • 105
  • 157
0

read this comment to know when it makes sense to have an instance and when not.

Community
  • 1
  • 1
Nazgul
  • 1,892
  • 1
  • 11
  • 15