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 are the three parts of a lambda expression?

524


What are the major advantages of internal iteration over external iteration?

563


What are passing parameters?

579


What are the advantages of inner classes?

498


You're given a Boolean 2D matrix, can you find the number of islands?

589






Give the difference between the println method and sqrt method?

619


What is consumer interface?

551


Can we create constructor in abstract class ?

572


Is null an object in java?

554


How does a for loop work java?

533


What is the tradeoff between using an unordered array versus an ordered array?

651


What is the difference between iterator and list iterator?

526


What is a java lambda expression?

545


How can we create objects if we make the constructor private ?

533


What is serial version uid and its importance in java?

592