This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Default Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Default Constructor" Watch "Default Constructor" New topic

Default Constructor

Jacob Michaels
Ranch Hand

Joined: Mar 23, 2003
Posts: 35
Can anyone tell me a good reason why a programmer would want to manually type the default constructor in his/her program when the JVM automatically inserts that code for you?
Thanks for any responses!
Joel McNary

Joined: Aug 20, 2001
Posts: 1821

There are a couple of reasons:
1). The programmer didn't type it in, it was generated by and IDE or other code-generation tool.
2). (More importantly): The programmer wants to be able to create default, blank objects even when there are other constructors present. Example:

won't actually work, because since another constructor is present, the compiler does not create the default constructor. You would have to add:

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Jacob Michaels
Ranch Hand

Joined: Mar 23, 2003
Posts: 35
Thanks for the reply!
public class Test
public Test() //must have because you overloaded the default constructor below. If you do not
{ //you will cause a compiler error
String displayString = "Hello, World";
public Test(String aString)
displayString = aString;
public void display()
public static void main(String argv[])
Test x = new Test();
Test y = new Test("You must have a default construtor");
[ October 09, 2003: Message edited by: Jacob Michaels ]
Herb Schildt
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
There is another reason why you might need to explicitly create a default (i.e., parameterless) constructor: When the construction of an object requires specific actions that the default constructor supplied by Java can't provide. For example, the constructor might need to initialize one or more instance variables, open a file, or obtain some resource. The default constructor provided by Java can't do these things. You must provide your own.

For my latest books on Java, including my Java Programming Cookbook, see
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
There may be occasions when you want to allow only a single instance of your class, or perhaps a small pool of them. In that case you create a default constructor but make it "private" instead of "public". Then you create a static method that other classes would use to get instances of your class. This is a useful pattern to use with "Factory" classes or other singletons.
If you look at the "java.lang.Math" class, you see that every method is "static", so it doesn't make sense to create an instance of the class. If you look at the source for it, you'll notice that they have a "private" constructor so that no one can instantiate it.
[ October 10, 2003: Message edited by: Wayne L Johnson ]
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Default Constructor