E J Chathuranga
E J Chathuranga

Reputation: 925

create static object from class that act like method

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!

Upvotes: 0

Views: 75

Answers (4)

manojsingh
manojsingh

Reputation: 61

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.

Upvotes: 0

Halayem Anis
Halayem Anis

Reputation: 7785

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]));
    }
}

Upvotes: 0

Shuwn Yuan Tee
Shuwn Yuan Tee

Reputation: 5748

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.

Upvotes: 1

geco17
geco17

Reputation: 5294

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

Upvotes: 0

Related Questions