aspose file tools
The moose likes Beginning Java and the fly likes class without main method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "class without main method" Watch "class without main method" New topic
Author

class without main method

Harjeet Dadwal
Greenhorn

Joined: Sep 09, 2001
Posts: 17
can I've class without main method ?
here I tried out
rectify the error please:-
class trial
{
public static void withoutMain()
{
System.out.println("Hello how are you !!!");
System.exit(0);
}
}
Where is the problem, help me ?
------------------
Harjeet Singh Dadwal


Harjeet Dadhwal
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
There is no problem, this class should compile without errors. But if you want to see "Hello how are you" displayed on your standard output, you have got to have a main method.
So what you can do is either to change your withoutMain into a main method, which must match the following :"public static void main(String args[])" (otherwise it won't be identified as the main method) or call the withoutMain method in the main method of this or another class:
public class RunIt {
public static void main(String args[]) {
trial.withoutMain();
}
}
W.
PS: By the way, your classname must start with a capital letter
public class Trial
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 757

No, the classname doesn't need to be capitalized.
The execution unit, a.java will compile fine.
Pho


Regards,

Pho
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
Ok, let's put it that way. It is heavily recommanded that the class names start with a capital letter as stated in the JLS :
6.8.2 Class and Interface Type Names
Names of class types should be descriptive nouns or noun phrases, not overly long, in mixed case with the first letter of each word capitalized. For example:

ClassLoader
SecurityManager
Thread
Dictionary
BufferedInputStream
Likewise, names of interface types should be short and descriptive, not overly long, in mixed case with the first letter of each word capitalized
Harjeet Dadwal
Greenhorn

Joined: Sep 09, 2001
Posts: 17
Sorry friend,
as you said that we'll have to define main method then it's not true, a static method first called by any class, what I've explained above, I've compiled it and 've executed successfully once, but what's the problem now, why it doesn't run now, I don't know ? but when will figure it out will tell you,
however thanks for your reply !!!

------------------
Harjeet Singh Dadwal
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
In order to create an application that executes at the command line, you need to invoke a class (a "driver class") that has a main method that matches the signature defined by the JVM.
Not all classes are expected to be the kick off point for an application. Those classes to not need to have main methods. They can however be used by the driver class.
If you want to see your code work, create a second class

Compile both.
Then run the Test class at the prompt
>java Test

[This message has been edited by Cindy Glass (edited September 28, 2001).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
Ruby V
Greenhorn

Joined: Sep 27, 2001
Posts: 13
Here is what I think:
1) Every class should have atleast one public class.
That means your class will compile and execute if you code it as follows:
public class trial
{
public static void withoutMain()
{
System.out.println("Hello how are you !!!");
System.exit(0);
}
}
2) However, to see a system out you need to have a main() like so:
public class trial
{
public static void main(String[] args){
System.out.println("Hello how are you !!!");
System.exit(0);
}
}

Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
No -every file does not need to have a public class.
A file can have AT MOST one public class, but zillions of class files are not public and still reside in their own file.
But you are correct, if he converts the wintoutMain method to a main method with a correct signature, then it would run if he did
>java trial
 
I agree. Here's the link: http://jrebel.com/download
 
subject: class without main method
 
Similar Threads
How to call a stand alone class from WAS???
can a program without "main" method show output?
how a class could be compiled with out any main method ?
how to run a normal java program without main method
Program without a main method