aspose file tools*
The moose likes Java in General and the fly likes constructor and main whichone will execute first.. 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 » Java in General
Bookmark "constructor and main whichone will execute first.." Watch "constructor and main whichone will execute first.." New topic
Author

constructor and main whichone will execute first..

Chenna Krishna
Ranch Hand

Joined: Dec 13, 2002
Posts: 34
Hi all,
constructor and main which one will execute first..
I am quite confusing..
Cheers
krish
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815
Depends how you mean. In a simple example, the main will execute first. Consider:

When the JVM starts, there are no objects created. Therefore, the JVM needs a starting point in the code. This is the main method, which is static--it belongs to a class, not a particular instance of the class.
The constructor is called when you create an object. When the above code says "new Test()" it invokes the constructor of the Test class.
However, only the main of one class is invoked. If you used another class ("OtherClass") that had a main in it, that main would not be called (unless you called it yourself in your code). The constructor would be called anytime you said "new OtherClass()"
Hope this clear up the usage between constructors and main methods.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Actually objects may be created before the invocation of main. Consider this example:

The static block will execute before main creating an instance of StaticTest before main is called.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Chenna Krishna
Ranch Hand

Joined: Dec 13, 2002
Posts: 34
Thanks all,
Here is the piece of code..
My constructor come's first and no instance of the class..

public MyClass(String[] args)
{
String err = null;

String thisName = Util.getProperty("");
String givenName = Util.getProperty("");


handler = new Handler();
handler.start();
}

.....
some couple of methods
.....
public static void main (String args[])
{
try
{

}
catch(Exception e)
{

}


}
///end
Now tell me which will execute first..I hope the main method
cheers
krishna
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
As long as you don't have any static initializtions in the class, then main should execute first.
Chris Treglio
Ranch Hand

Joined: Jun 18, 2001
Posts: 64
With the code you've posted, with the main method empty the way it is, if you run this class alone (at the command line, like "c:\java MyClass") your constructor won't ever execute.
Likewise, if you are instantiating this class from another class (like MyClass m = new MyClass("hi Chenna")) your main method won't ever execute.
If this class is the entry to your application, typically you instantiate your class (call your constructor, with the new keyword) from its own main method, like Mike did in his example.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructor and main whichone will execute first..
 
Similar Threads
Which constructor is being called?
initialiser block/constructor
initialization of final instance fields
Constructor Vs Static Block
static, instance, constructor call order