You can write a class in java without main() method as here
class DemoFirst{
static{
System.out.println("Hello Friends....");
}
}
and you can execute this class as well , But here is a thing to consider is, " It is possible with java jdk-1.5 and lower versions ". If you execute this class with jdk-1.6 or higher version you will get an error.
------------------------------------------------------------------------------------------------------------------------
Compilation and Execution :
D:\Training Data>javac DemoFirst.java
D:\Training Data>java DemoFirst
Error: Main method not found in class DemoFirst, please define the main method as:
public static void main(String[] args)
class DemoFirst{
static{
System.out.println("Hello Friends....");
}
}
and you can execute this class as well , But here is a thing to consider is, " It is possible with java jdk-1.5 and lower versions ". If you execute this class with jdk-1.6 or higher version you will get an error.
------------------------------------------------------------------------------------------------------------------------
Compilation and Execution :
D:\Training Data>javac DemoFirst.java
D:\Training Data>java DemoFirst
Error: Main method not found in class DemoFirst, please define the main method as:
public static void main(String[] args)