• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class and public

 
kiranv
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
vivek rai
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome aboard Vivek.

Ajith
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic