aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why main method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why main method" Watch "why main method" New topic
Author

why main method

vijay Thiru
Greenhorn

Joined: Jan 02, 2006
Posts: 19
public class sample1 {
int as;
static int s;
int c ;
static {
s = 3;
sample1 s = new sample1();

System.out.println(s.as);
System.out.println(s.c);
System.out.println(s.s);
}
{
as =23;
}
sample1() {
c =23;
}
public static void main(String[] args) {
/*sample1 s = new sample1();

System.out.println(s.as);
System.out.println(s.c);
System.out.println(s.s);*/
}
}
the above program, i just commented the main method defination.then i copied the main method defination into static block.
both the things give the same result.
then why we have main method.we start execution from static block itself.
what is difference between static block and static main method execution.
please expalin me reason for this.
vijay Thiru
Greenhorn

Joined: Jan 02, 2006
Posts: 19
the above program itself,
i have constructor and instance initializer block.
both doing the same thing.
what is the difference between constructor initialization and instance initializer block.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
when u execute java sample1, it means u want to execute sample1.main(args)

Following are the steps of execution....

1. First ClassLoader loads the class.

2. Then after that class gets initialised means all the static blocks and static variables gets executed in their order of appearance in the code. Remember no static method is executed at this time.

3. After the initialisation of class, java interpreter runs ur class by calling

main method.

Program starts when main starts.
Ptogram ends when main ends.

I hope u got ur reply.

Thanks

Naseem


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: why main method