• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

"One file has to have one and only one public class", is it right?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"One file has to have one and only one public class", is it right?
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes thats right

Originally posted by Han Shu:
"One file has to have one and only one public class", is it right?


 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Erm well actually
class Test{
public static void main(String[] args){}
}
does compile and run (without output). Does this show that "public" keyowrd if absent is implicitly added to top-level class at compile time? Someone please clarify.
Thanks
Adam
 
Han Shu
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even this one can be compiled without error? I am really confused.
package MyPackage;
class P1{
void afancymethod(){
System.out.println("What a fancy method");
}
}
 
Roopa Bagur
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I misunderstood your question.
It is true that you can have only one public class in a file. If you have more than one the compiler will complain
But you don't have to have a public class. When you don't have an access modifier for a class the default is used.Thats the reason for your code to compile with out an error.

Originally posted by Han Shu:
Even this one can be compiled without error? I am really confused.
package MyPackage;
class P1{
void afancymethod(){
System.out.println("What a fancy method");
}
}


 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
A .java class must contain MAXIMUM one public class.
Try these and run each class with "java classname" you'll see the results.
NoPublic.java

TestNoPublic.java

You will have to adjust your package names, AND, the import statement in TestNoPublic to get a good instance of NoPublic.
Hope this helps.

------------------
SOURCE CODE should be SURROUNDED by "code" tags.
Click here for an example
[This message has been edited by John Bateman (edited September 06, 2001).]
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Shu, one file can contain none or many classes as well as interfaces.
At most one of them can be defined with public modifier. If there is one(either public class or public interface), the file name must be the same as that public class or interface in order to compile properly.

Originally posted by Han Shu:
"One file has to have one and only one public class", is it right?



------------------
Guoqiao Sun
Sun Certified Programmer for Java™ 2 Platform
try my mock exam¹² at my homepage.
 
reply
    Bookmark Topic Watch Topic
  • New Topic