If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: Java class without main method?

You need a main() method if you want to start the class with the java command:
java TheClass
You don't need a main() method if another class creates an instance of that class:
TheClass aref = new TheClass();
also an applet does not use a main() method.

Re: Java class without main method?

Re: Java class without main method?

Blocks are two types one is static and non-static,
when u use static blocks inside the class till Jdk 1.6 version it will execute with out main(), but from jdk 1.7 version it wont execute. all execution starts from main(). but always static block will execute first.

Re: Java class without main method?

Originally Posted by DineshPazhaniyandi

Blocks are two types one is static and non-static,
when u use static blocks inside the class till Jdk 1.6 version it will execute with out main(), but from jdk 1.7 version it wont execute. all execution starts from main(). but always static block will execute first.

But i am using jdk 1.7 and what do you mean by

static block will execute first

What do you want to say? Will it be executed or not?

I am running the above code successfully but eclipse is not able to run it.

Re: Java class without main method?

first you check jdk version, if jdk 1.7 you are using.. It wont execute.
first , I executed this program with cmd,
This is the result
D:\>javac WithoutMain.java

D:\>java WithoutMain
Error: Main method not found in class WithoutMain, please define the main method
as:
public static void main(String[] args)
.............................................................................................................................
With eclipse:

Error: Main method not found in class WithoutMain, please define the main method as:
public static void main(String[] args)
...............................................................................................................................

if you are using jdk version below 1.7, then static block will execute with out main().
..................................................................................................................................