This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructor and main whichone will execute first..

 
Chenna Krishna
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
constructor and main which one will execute first..
I am quite confusing..
Cheers
krish
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chenna Krishna
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as you don't have any static initializtions in the class, then main should execute first.
 
Chris Treglio
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic