• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Default Constructor

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Author
Posts: 253
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Hey, I'm supposed to be the guide! Wait up! No fair! You have the tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic