File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes public class for calling main Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "public class for calling main" Watch "public class for calling main" New topic
Author

public class for calling main

Bernd Stransky
Ranch Hand

Joined: Nov 20, 2001
Posts: 47
Question about public class:
Code example:
class A { public static void main(String a[]) {} }
This code in file A.java compiles and runs with:
java A
What is the rule for having a public class in a java file?
I know the difference to <default>, protected and private, but I always thought that you need to have a public class (in this case public class A) when calling main in a Java application.
Any help is appreciated,
Bernd
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Originally posted by Bernd Stransky:
Question about public class:
Code example:
class A { public static void main(String a[]) {} }
This code in file A.java compiles and runs with:
java A
What is the rule for having a public class in a java file?
I know the difference to <default>, protected and private, but I always thought that you need to have a public class (in this case public class A) when calling main in a Java application.
Any help is appreciated,
Bernd

Well its like this..
You can have the file name to be any name refering to any class name in a source file. If you do that and the class name you had used to save it doesnt have a main method, it will compile though but it may not run and give a runtime exception
But..
If you have a class as public, you must save the file in the class name that's defined public
HIH
Ragu
Bernd Stransky
Ranch Hand

Joined: Nov 20, 2001
Posts: 47
Originally posted by Ragu Sivaraman:
Well its like this..
You can have the file name to be any name refering to any class name in a source file. If you do that and the class name you had used to save it doesnt have a main method, it will compile though but it may not run and give a runtime exception
But..
If you have a class as public, you must save the file in the class name that's defined public
HIH
Ragu

Thanks you - I was under the impression that public is mandatory for the class with main() method - OK, it works without as well.
-Bernd
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: public class for calling main
 
Similar Threads
Why this code compiles?
clone and java.io.File
please help me for this package question
Problem in merging PPT files using java
Do you need a main method for your application to run?