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 class and public Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "class and public" Watch "class and public" New topic
Author

class and public

kiranv
Greenhorn

Joined: May 08, 2000
Posts: 7
Hello,
I have the following code :
Filename:: public_test.java
class test1 {
private void amethod(){
System.out.println("amthod");
}
}

class public_test extends Object
{
public static void main(String args[]){
System.out.println("helo this is public_test");
}
}

Both the above classes are in the same file public_test.java and the program compiles and works fine.
Now, I had read before that when you have more than one class in a file, one of them needs to be declared public.
Here I did not declare any class as public, still it compiles and runs fine.
Pl explain
thanks
kiran
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Kiran,
You can have as many default classes as you want in your file. But ONLY ONE OF THEM can be public. Now if you have A and B, two default classes in your file and file name is A.java, then main method of A.java will execute only.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hie,
Just to put it differently, YOU DO NOT HAVE TO declare any class public in a file, even if you have so many them. But if you decide to, then ONLY ONE can be declared public.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

Just wanted to add ....
when you declare one of the classes "public", then the
filename SHOULD be the name of that class followed by
.java extn. In the above example, if you declare the
class "public_test" as public, your file should be named
"public_test.java" (which ofcourse you did).
Regds.
- satya
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Sunny
i tried to save the above classes as test1.java and then after sucessfully compiling i tried saying "java public_test" and the main executes fine. thus u r wrong when u say "Now if you have A and B, two default classes in your file and file name is A.java, then main method of A.java will execute only."
thus it is not necessary that the main method of A.java will execute only. if B also has a main method it can too be executed.

please check it out.

[This message has been edited by rahul_mkar (edited May 08, 2000).]
kiranv
Greenhorn

Joined: May 08, 2000
Posts: 7
Rahul,
It worked because you said 'java public_test' and not
'java test1'(you already had public_test.class)
I think it will not compile unless you have the main method in test1 class and the file name test1.java..
It looks like only one class declaration CAN have a public declaration in a file with more than class, it is not mandatory..
kiran
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Kiran,
In order for a class to run( ie., for the JVM to search
an entry point and start executing the code, ) the only requirement is that the class should have a correctly formed main method. For instance, even classes declared abstract/final with a correctly formed main method can be run by JVM.
Note that the class need not be public, or be declared in PublicClassname.java file.
It is worth giving these concepts some extra weightage as this has appeared in the mock exams more than often, and people tend to make mistakes here.
HTH

Ajith
[This message has been edited by Ajith Kallambella (edited May 09, 2000).]
[This message has been edited by Ajith Kallambella (edited May 09, 2000).]


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
vivek rai
Ranch Hand

Joined: May 08, 2000
Posts: 45
Yes, ( btw, Hi, i'm vivek, a new guy in town.. new to java - so excuse/correct me .. )
i had the following in a file called test3.java
//test3.java
class test1
{
public static void main(String args[])
{
System.out.println("test1" + args[0]);
}
}
class test2
{
public static void main(String args[])
{
System.out.println("test2" + args[0]);
}
}
compling the above produced two class files test1.class and test2.class ..
both can be invoked :
$ java test1 abc
test1abc
$ java test2 cdf
test2cdf
I guess ( see chapter 12 of JLS ), that jvm just tries to load and run the main of the specified class, so if that ( loading+main() method ) is ok, it doesnt matter how they were written in a file before getting complied.
cheers,
vivek
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Welcome aboard Vivek.

Ajith
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class and public