• 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

When do we need to declare a class as public?

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a coding detail I ran into this morning that surprised me. By mistake, I compiled and ran the following, in the file A.java:

class A {
public static void main(String[] args) {
System.out.println("Hello");
}
}

Then, I noticed that class A was not declared "public".

I'm so used to reading stuff like: "A java source file must contain one public class, which has the same name as the source file."

I see that I need to leave "main" as public; after all, main must be called from the "outside". But wouldn't we say the same thing about class A, that the class must be visible from the "outside"? (Outside the package.)
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well the rule is simple. Any .java file can have any number of non-public top-level (i.e. not inner) classes. But if there is any public top level class (i.e. non-inner again), then it must be created in a .java file whose name matches the class name. i.e. if the class is public class MyClass, then it must be declared in MyClass.java while a class declared as class NonPublic can be declared in a file named AnyName.java.

The thing to note is that if you create a class like this



And you create this in a file named My.java, then you will compile it as javac My.java, but to run it, you'll have to use the command java MyClass

Note that as I pointed, this only applied to top level classes. Inner public classes don't affect the filename that can be used with them...
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent explanation, Ankit. Many people are surprised at a couple of the points that you talked about.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:Excellent explanation, Ankit. Many people are surprised at a couple of the points that you talked about.



I'm sure you knew everything that I said
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:

Ruben Soto wrote:Excellent explanation, Ankit. Many people are surprised at a couple of the points that you talked about.



I'm sure you knew everything that I said


But many people don't know it. I figured it out from some other discussions in the forum a while back, where someone was trying to launch the main() class which was in a file whose name was different by using the name of the file.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
btw, the only access modifiers permitted to use in top level classes is public, and default access.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic