• Post Reply Bookmark Topic Watch Topic
  • New Topic

No public constructors??  RSS feed

 
E Ni
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me why a class would have no public constructors?
How would you create such a class and how would methods of a class be called without creating an object of that class?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can think of several reasons why a class would have no public constructors. First, if the class has all static methods, then you never need an instance of the class. The java.lang.Math class is a prime example. In such a case, the class exists only to contain a group of related functions, but they don't actually need an object to act on.
A second reason for no public constructors is if you want to limit the creation of an object. There are several design patterns that create objects in specific ways and public constructor is not desired. The simplest example here is the Singleton pattern. This is when you only want one instance of the object to exist for the whole program. In this case, there is usually a static method which returns the instance of the object. This method may create the object, if necessary, as well.
There are several other creational design patterns where you want the user of the class to call a certain function in order to create the object in a certain way. In such cases, you wouldn't need a public constructor; the object is created through some other means.
If you need more information, I suggest that you check out the Design Patterns board here at the Ranch. They can probably give you more details about such issues.
I hope this answers your question.
Layne
 
Chris Harris
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Welcome to the Javaranch.
There are a number of reason why you may use a non-pubic constructor.
1) Only ever having a single instance of you object (The singleton pattern). For example :
You want to same object to to used though out your app but you don't want to pass it everywhere. Here is some code (I am wirtting it in this box):

Ok this is not a very useful class but you see what I mean. In you other classes that need a the same instance of you "Singleton", call the method getInstance() then call the object methods. This is a very useful pattern in large projects.
2) A utility class that would there would never want to create a instance of. For example you may just want a place to store a load of constances.

If you need to get the value of hello then do Values.HELLO.
There are a number of other reason, so if you are interested please say.
Hope that helps
Chris.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Welcome to JavaRanch!
There are, broadly, two reasons why a class will have no accessible constructors. First, it may be a class, instances of which should never be created. The java.lang.System and java.lang.Math classes are like this. All the members of these classes are static, so there's no reason to ever create an instance of either. To make this explicit, they both include a private constructor to prevent them from ever being created.
Second, the class may want to control how it is created. For example, a Singleton class is a class that ensures that one, and only one, instance exists. The java.lang.Runtime class is like this. There's one single static instance you can get by calling Runtime.getRuntime(), but you can't make your own instances. The only class that calls the private constructor is Runtime itself.
Other classes may allow more than one instance, but want to cache and reuse instances; again, by using a private constructor and a static "getInstance()" method, this is easy to do. An example from the JDK is the java.lang.Class class. The method Class.forName() can return either a new Class object, or a previously-created one.
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It could even be simpler. If you write a class without writing any constructor for it, the compiler will automatically generate a default constructor without any arguments that is called to instantiate this class. However, write any constructor at all with whatever arguments, and the compiler will not generate any default constructor.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!