mercredi 24 janvier 2018

Does this class implement singleton design pattern? [on hold]

From Java in a Nutshell

package javanut6.ch01;

import java.io.IOException;

/*
 * A standard scratch pad class that I often use when exploring a simple
 * java library or reminding myself of how Java langauge features interact
 *
 */
public class ScratchImpl {

    private static ScratchImpl instance = null;

    // Constructor
    public ScratchImpl() {
        super();
    }

    /*
     * This is where your actual code will go
     */
    private void run() {

    }

    /**
     * @param args
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws IOException {
        instance = new ScratchImpl();
        instance.run();
    }

}

I wonder the following questions:

why does it define a private field which is an instance of the class itself?

Does the class implement the singleton design pattern?

What is the purpose of not initializing the field in the constructor, but in main method?

Why does the constructor call super(), since there is no explicit super class?

What is the purpose of making main a method of the class, instead of a method of another class?

Thanks.

Aucun commentaire:

Enregistrer un commentaire