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

Can a method be overloaded based on different return type but same argument type?

759


Explain java code for recursive solution's base case?

805


How many types of string data types are there?

777


What is static class

847


What are checked exceptions?

821


What is the purpose of garbage collection in java?

864


Can a vector contain heterogenous objects?

825


What are keywords give examples?

825


Which is faster call by value or call by reference?

724


What is the use of optional ?

824


Is arraylist dynamic in java?

736


How many ways can an argument be passed to a subroutine and explain them?

806


What is a heavyweight component?

776


What is lambda in java?

774


What is final variable?

712