This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Quest About Constructors?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Quest About Constructors??" Watch "Quest About Constructors??" New topic
Author

Quest About Constructors??

Azam Bukhari
Ranch Hand

Joined: Oct 03, 2000
Posts: 58
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

Don't wanna invent the wheel again!!
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
<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.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Sadaf Zaidi
Greenhorn

Joined: Oct 09, 2001
Posts: 29
Thank you Marilyn deQueiroz .
Peter Simard
Ranch Hand

Joined: Oct 31, 2001
Posts: 54
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


PAS<br />peter@panvox.net<br />2b || !2b
 
jQuery in Action, 2nd edition
 
subject: Quest About Constructors??
 
Similar Threads
Instantiating a class with private constructor
SCJP Brainteaser (5)
Protecting Applet Class Files from Tempering by Users
What is the super() constructor of Object ?
Can a constructor return?