could you run the java program without main method?

Answer Posted / amit singh

yes you can run java peogram without main
we do it through the static block ,because its execute
before the main method,
so when the class is loaded it will execute before main
but whrn the static block excute and after exiting it jvm
search for main when it will not fou8nd it then it throw
some exception so for this we use the
System.exit();
class Moon
{
static
{
System.out.println("hello i am visible without main");
}
System.exit();
}
Thanks amit singh

amitsing2008@gmail.com

Is This Answer Correct ?    36 Yes 8 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What do you know about the garbage collector?

694


Explain about OOPS concepts and fundamentals.

695


What is a subsequence of a string?

662


How does the garbage collector works in java?

657


Can java arraylist hold different types?

608






Difference between string s= new string (); and string s = "abv";?

842


Why is a string immutable?

649


How do you use parseint in java?

603


What is scope & storage allocation of static, local and register variables? Explain with an example.

681


How many types of design patterns are there?

634


What is a treeset class?

653


What is void in java?

697


Why java is object oriented?

678


How does map works in java?

624


What are abstract methods in java?

769