• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

class without main method

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the classname doesn't need to be capitalized.
The execution unit, a.java will compile fine.
Pho
 
Wilfried LAURENT
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
You have to be odd to be #1 - Seuss. An odd little ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic