Tuesday 29 October 2013

CAN WE EXECUTE A PROGRAM WITHOUT THE MAIN METHOD ???????

ANSWER TO THE ABOVE LINE IS YES...

I will explain you how it is possible later but before that i want to explain you a main concept of java program that is Static Block.

Static Block:-
            Whenever a block is declared with "static" keyword, it is known as static block in the program..

  • Nature:-
  1. The code inside static block get executed before the execution of main method.
  2. Static block cannot be declared inside any method.
  3. Static block is executed only once
Program:-
class Demo
{
static 
{
System.out.println("It is a static block");
}
public static void main(string args[])
{
System.out.println("It is main method");
}
}

Output:-
It is a static block.
It is main method.

Note:-The line written in pink color in the above program is a static block.

Hope you got the answer of the topic that can we execute a java program without the main method???
The answer is very clear..Yes we can execute a java program without main method with the help of Static Block..
I can give you an example in support of this explanation..
Example: A java program without main method.
class Demo
{
static
{
System.out.println("Hello my reader");
System.exit(0);
}
}

Output:-
Hello my reader

Hence it is clear that we can execute a java program without the main method..

No comments:

Post a Comment