0

I'm bit confused with the scripts that I saw recently. I want some explanation about it. I googled it and found that, this mechanism is being used from while but I couldn't understand it . Please don't downgrade my question if there any wrong.

I'm an android developer and start to being legend. :D

public final class ClassName{

    public static ClassName initSDK(@NonNull @GuiContext Context context) {
        return new ClassName(context);
    }   

    private ClassName(Context guiContext) {
        startSDK(guiContext);
    }

}

what is initSDK. how it's call and what is the mechanism?

Thank you for your valuable time!

8
  • 2
    initSDK is the name of a method. stop doing Android, start with the basics of Java instead. Commented Jun 11, 2018 at 9:26
  • Looks like you're just asking what a "factory method" is. Commented Jun 11, 2018 at 9:26
  • 1
    @David Perhaps not. The annotations here looks important - there might be a framework involved that calls this method. Commented Jun 11, 2018 at 9:28
  • 1
    @EJChathuranga how else would you get an instance of ClassName in another class? the constructor is private, so it's not possible to retrieve one from the constructor, that's why you need to call that method. Commented Jun 11, 2018 at 9:34
  • 1
    @EJChathuranga it resembles a Singleton, but it isn't one, Factory can be like this. Commented Jun 11, 2018 at 9:34

4 Answers 4

1

initSDK here is a static method, which you call it through it's class name, such as:

ClassName instance1 = ClassName.initSDK(context);

Internally, it creates an object instance of ClassName & return it. For example, instance1 here is an instance of ClassName.

Note that the class constructor private ClassName(Context guiContext) { .. } is declared private, which means you can't instantiate this object via below method:

// Wrong, can't instantiate object this way. Constructor is declared "private"
ClassName instance2 = new ClassName(context);

Similar to initSDK, sometimes this similar method is named getInstance(), which indicates get me an instance of the object, access through the package name.

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah, What the purpose of private constructor? I think we can use directly singleton pattern instead of above mechanism.
@EJChathuranga This is not singleton. Everytime ClassName.initSDK(context); is called, a different instance of ClassName object is returned, as it new an object each time. Whereas for singleton, it only new the object first time, subsequent call will return the same object instance.
Then why constructor is private? What the purpose?
Is this kind of security protocol?
0

You can call it with ClassName.initSDK() from another class. It's a static method. Refer to the documentation.

Comments

0

This is a creational pattern called Factory Pattern, it hides the creation logic

Example of usage:

public interface MyFile {
    public String getContent(String filename);
}

public class MyCSVFile impelments MyFile {
    public String getContent(String filename) {
        // proper implementation on how to open and read CSV file
    }   
}

public class MyPDFFile impelments MyFile {
    public String getContent(String filename) {
        // proper implementation on how to open and read PDF file
    }   
}


public class MyFactory {

    public MyFile factory(String filename) {
        String ext = // utility to get file extension ...
        if (ext.equals("pdf")) {
            return new MyCSVFile(filename);
        }
        if (ext.equals("csv")) {
            return new MyPDFFile(filename);
        }

        // unhandled operation ? setup a default file reader ?
        // ...
    }
}

public class MyBusinessClass {

    public static void main(String[] args) {
        MyFile myFile = new MyFactory().factory(args[1]);
        System.out.println(myFile.getContent(args[1]));
    }
}

Comments

0

initSDK is a factory method to produce an instance of ClassName as its constructor is private and cannot be invoked from outside. So we need some public which can be accessed without creating a new instance, therefore public static type method is available.

Why not to make the constructor private? Because giving factory is intended to control object creation mechanism.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.