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
posted
0
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
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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