This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Quest About Constructors??

 
Azam Bukhari
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Community,
I wanna querry about the workin of constructors:-
1. Will a constructor fires itself(execute itself, i mean)
if there is no object of the class...e.g a simple program of "hello World" ...i believe that although there is no object required for the miraculous output of hello world...but the class still do have a constructor...or isn't it!
What will be the nature of this constructor...?
Regards
Azam Bukhari
azambukhari@hotmail.com
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<pre>class HelloWorld
{
public static void main( String[] args )
{
System.out.println( "Hello World" );
}
}
</pre>

This class has a default constructor that looks like

<pre>
HelloWorld()
{
super();
}
</pre>

but it is never used because the HelloWorld class is never instantiated.
 
Sadaf Zaidi
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Marilyn deQueiroz .
 
Peter Simard
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler gives you a default constructor for every class, with this caveat: if you don't define ANY constructors the compiler will generate one for you and intialize all the prroperties to null or zero, depending on their type.
If you do define your own constructors, and you should, then YOU must define the default constructor, also known as the "no argument constructor."
The compiler does it this way because the presence of any programmer defined constructors indicates the programmer does not want the default constructor. So, if you define your own constructors, and also want the no argument constructor, then you must re-define the no argument constructor.
Pete
------------------
PAS
peter@panvox.net
2b | | !2b
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic