Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Default Constructor

 
Jacob Michaels
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Jacob Michaels
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()
{
System.out.println(displayString);
}
public static void main(String argv[])
{
Test x = new Test();
x.display();
Test y = new Test("You must have a default construtor");
y.display();
}
}
[ October 09, 2003: Message edited by: Jacob Michaels ]
 
Herb Schildt
Author
Ranch Hand
Posts: 253
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob:
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.
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic