Java private constructor and getInstance() method. Why?

Status
You're currently viewing only Bagheera's posts. Click here to go back to viewing the entire thread.
Not open for further replies.

Bagheera

Ars Tribunus Angusticlavius
8,773
Subscriptor
Here's a topic I don't understand. What's the benefit of making a private constructor and a public getInstance method?

I understand why you would make the constructor private: to keep someone from instantiating a class. The correct term is "Utility class", right? The example I get is the Math class. Programmers should have access to Math.sqrt(), Math.random() and other methods, but they should never be able to write "Math myObject = new Math();"

But why make a public method getInstance()? Take the Calendar object, for example. You can't write "Calendar c = new Calendar()". You have to write "Calendar c = Calendar.getInstance()". But it has the exact same result: an object of type Calendar with a reference named "c".

Some research pointed me toward the "Factory Design Pattern," explained here. The concept is that you create multiple "getInstance" methods that return Objects with different properties based on the parameters of the method call. But couldn't you achieve the same objective with overloaded constructors?

Further research pointed me toward "Singletons", which ensure that only one instance of a class is instantiated. I can see a scenario where you'd want that. The getInstance() method checks to see if an object of that class already exists in the program. It returns null if an object exists. Couldn't you build that logic into the constructor?

I'm definitely missing something here. The paragraphs aren't arguing against getInstance methods. There to demonstrate that I've researched the topic and still don't get it. Can a friendly Arsian give me an example where a getInstance method works better than a public constructor(s)?
 

Bagheera

Ars Tribunus Angusticlavius
8,773
Subscriptor
Vince-RA, dlp. Thanks, I hadn't thought about that. So if you want to build a singleton, you can't use the constructor. Got it.

How about classes that permit multiple instances? Take Java's Calendar and DateFormat classes (but oddly not their GregorianCalendar and SimpleDateFormat classes). You can have any number of Calendars and DateFormats in a program. What's the logic behind making their constructors private?
 

Bagheera

Ars Tribunus Angusticlavius
8,773
Subscriptor
[url=http:/civis/viewtopic.php?p=25786041#p25786041:1k2rksdf said:
andrewme[/url]":1k2rksdf]
[url=http:/civis/viewtopic.php?p=25785119#p25785119:1k2rksdf said:
Bagheera[/url]":1k2rksdf]
The getInstance() method checks to see if an object of that class already exists in the program. It returns null if an object exists.

Never seen it done that way - typically the singleton accessor returns (a reference / pointer to) the single instance to every requester. This means every requester in the process can easily access the instance without having to clutter up the code passing it around or resorting to global variables.

I think I'm following. So calling
Code:
Singleton a = Singleton.getInstance();
Singleton b = Singleton.getInstance();
Singleton c = Singleton.getInstance();

would result in three variables all pointing to the same Singleton object. The first time it's called, the getInstance method creates the new Singleton. The other times, it returns the object that already exists. That makes sense.
 

Bagheera

Ars Tribunus Angusticlavius
8,773
Subscriptor
[url=http:/civis/viewtopic.php?p=25787729#p25787729:2770d0m1 said:
Faramir[/url]":2770d0m1]I don't really know anything about certifications, but it seems very strange that they want you to take an exam on 6, when it's been completely deprecated. You can't even get oracle jvms for it anymore unless you pay for them. Not that 7 is hugely different, but why don't they assign a test for the current version?

The courses are slightly outdated. My last course was on Perl, yet there's no Python course...Either way, the test is a bitch, with a passing score of only 61%.
 
Status
You're currently viewing only Bagheera's posts. Click here to go back to viewing the entire thread.
Not open for further replies.