aspose file tools*
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 Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Default Constructor" Watch "Default Constructor" New topic
Author

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
Bartender

Joined: Aug 20, 2001
Posts: 1815
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()
{
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

Joined: Oct 01, 2003
Posts: 239
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.


For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Default Constructor
 
Similar Threads
Constructor of a Servlet ?
this(Color.Red) ?
Spring: Hello World Does Not Work
Implicit Constructor
Which statement is true for default Constructor?