user2144013
user2144013

Reputation: 35

Use of a private constructor

I'm a begginer programmer for Android and I found some code over the internet and I couldn't get what this "Class not meant to be instantiated" means?! Also what's the use of it. I would be very happy if somebody could help here.

public class Settings
{
    //some code
    private Settings() {} // Class not meant to be instantiated
    //some code
}

Upvotes: 1

Views: 1246

Answers (7)

Sarath Govind
Sarath Govind

Reputation: 133

We can have private constructor . Below program depicts the use of private constructor with a static function

class PrivateConstructor {
    private:
    PrivateConstructor(){
        cout << "constructor called" << endl;
    }
public:
    static void display() {
        PrivateConstructor();
    }
};
int main() {
    PrivateConstructor::display();
}

Upvotes: -1

Malwaregeek
Malwaregeek

Reputation: 2274

As other have mentioned, a class having private constructors cannot be instantiated from outside the class. A static method can be used in this case.

class Demo
{

    private Demo()
    {
    }

 static void createObjects()
    {
       Demo o = new Demo();
    }

}

class Test
{
   public static void main (String ...ar)
{
    Demo.createObjects();
}

}

Upvotes: 0

Phil
Phil

Reputation: 36289

It is a private constructor. This means that outside classes cannot create new instances using the default constructor.

A little more info

All Objects in Java have a default constructor:

public MyObject() {}

That is how you can have this class:

public class MyObject{}

and still be able to call:

MyObject mObj = new MyObject();

Private Constructors

Sometimes a developer may not want this default constructor to be visible. Adding any other constructor will nullify this constructor. This can either be a declared constructor with empty parameters (with any of the visibility modifiers) or it can be a different constructor all together.

In the case above, it is likely that one of the following models is followed:

  1. The Settings object is instantiated within the Settings class, and is where all the code is run (a common model for Java - where such a class would also contain a static main(String[] args) method).

  2. The Settings object has other, public constructors.

  3. The Settings object is a Singleton, whereby one static instance of the Settings Object is provided to Objects through an accessor method. For example:


public class MyObject {
    private static MyObject instance;
    private MyObject(){}//overrides the default constructor
    public static MyObject sharedMyObject() {
        if (instance == null)
            instance = new MyObject();//calls the private constructor
        return instance;
    }
}

Upvotes: 3

pillingworth
pillingworth

Reputation: 3238

The constructor is private so only the class itself can create instances. There are several reasons for doing this. A couple off the top of my head...

  1. The class is a "utility" class that only contains static methods and so instantiating it would make no sense. As the class is commented "Class not meant to be instantiated" I guess this is the most likely reason.

  2. The class itself controls its own lifecycle and provides methods for creating instances. For example if the class is a lazy singleton it might provide a method that creates an instance when first called and return this instance on subsequent calls.

Upvotes: 8

Seva Alekseyev
Seva Alekseyev

Reputation: 61370

It's not a nested class, it's a constructor. A private constructor means that you can't construct instances of this class from outside, like this:

Settings s = new Settings(); //Compilation error! :(

Now, if a class can't be instantiated, what could it be for? The most likely reason for this is that the class would return instances of itself from a static method, probably as a singleton. The settings are normally global to the program, so a singleton pattern really fits here. So there would be a static method that goes kind of like this

static private TheOnlySettings = null;

static public getSettings()
{
    if(TheOnlySettings == null)
        TheOnlySettings = new Settings(); //Legal, since it's inside the Settings class
    return TheOnlySettings;
}

See if that's indeed the case.

Upvotes: 1

dudebrobro
dudebrobro

Reputation: 1317

The constructor is private so its not meant to be called by anything outside of the class

Upvotes: 1

Henry
Henry

Reputation: 43738

This inner construct

private Settings() {}

is a constructor for Settings instances. Since it is private, nobody can access it (outside of the class itself) and therefore no instances can be created.

Upvotes: 3

Related Questions