-
October 29th, 2013, 03:58 AM
#1
Java class without main method?
Is it possible to have a class with no main method in Java and still working perfectly??
-
October 30th, 2013, 06:44 AM
#2
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.
What does "work perfectly" mean?
Norm
-
October 31st, 2013, 01:05 AM
#3
Re: Java class without main method?
Thank you Norm for your valuable reply, but if main method is required to start a program in Java then my problem is with this code:
class WithoutMain
{
static {
System.out.println("Hello Java");
System.exit(0);
}
}
It is working without any error or warning on dos. But eclipse is not able to run it. If this code is working in command prompt, why is it not so with eclipse?
-
October 31st, 2013, 05:39 AM
#4
Re: Java class without main method?
Sorry, I don't know anything about the IDE.
I never use static blocks so I forgot about them. I always use a main() method in any class I want to execute with the java command.
Norm
-
November 1st, 2013, 01:16 AM
#5
Re: Java class without main method?
Thank you Norm for your replies, I really appreciate it.
-
November 28th, 2013, 06:30 AM
#6
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.
-
December 3rd, 2013, 05:19 AM
#7
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.
-
December 3rd, 2013, 10:58 PM
#8
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().
..................................................................................................................................
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|