File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Quest About Constructors??" Watch "Quest About Constructors??" New topic

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...?
Azam Bukhari

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

Joined: Jul 22, 2000
Posts: 9059
<pre>class HelloWorld
public static void main( String[] args )
System.out.println( "Hello World" );

This class has a default constructor that looks like


but it is never used because the HelloWorld class is never instantiated.

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

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.
2b | | !2b

PAS<br /><br />2b || !2b
I agree. Here's the link:
subject: Quest About Constructors??
It's not a secret anymore!