anon1981
anon1981

Reputation: 111

Java compilation of a .java file without a public class

Okay, so a java source file must have at least one public class and the file should be called "class-name.java". Fair enough.

Hence, if I have a class, then the following would compile:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}

But what bugs me is that if I remove the 'public' access modifier from the above code, the code still compiles. I just don't get it. Removing it, the code looks like:

class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}

In the above code, since I removed the public access modifier, my class has default or package access, i.e. it can't be accessed from the outside world, only from within the package.

So my question is, how does the above code compile ? The file HelloWorld.java in this case does not have a public HelloWorld class (only a package-private HelloWorld.class) and thus to my understanding should not compile.

Upvotes: 8

Views: 30846

Answers (6)

xong
xong

Reputation: 350

That's nothing to wonder about. I suppose this behavior is similar to the one of some C/C++-compiler.

Code like "void main() { /.../ }" will be compiled correctly by those compilers, although it is not standards-compliant code. Simply said, the compiler exchanges the "void" with "int".

I think a similar behavior is implemented by the java compiler.

Upvotes: 0

java_mouse
java_mouse

Reputation: 2109

There are valid usages for a non public classes. So the compiler does not give error when you try to compile the file.

Upvotes: 1

Mathias Schwarz
Mathias Schwarz

Reputation: 7197

A main method is just like any other method. The only difference is that it may be invoked from the command line with the java command. Even if the main method is not visible from the command line, the class can still be used like any other Java class, and your main method may be invoked by another class in the same package. Therefore i makes sense that it compiles.

In Java main function are not special in any sense. There just exists a terminal command that is able to invoke static methods called main...

Upvotes: 1

Crollster
Crollster

Reputation: 2771

When you do not specify the access modifier of the class (or its field or method), it is assigned "default" access. This means it is only accessible from within the same package (in this case, the default package).

The website Javabeginner.com has an article on the subject - you should become familiar with access modifiers in Java, either from this site, or others.

Upvotes: -1

Qwerky
Qwerky

Reputation: 18435

a java source file must have at least one public class and the file should be called class-name.java

Incorrect, a top level class does not have to be declared public. The JLS states;

If a top level class or interface type is not declared public, then it may be accessed only from within the package in which it is declared.

See http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#104285 section 6.6.1.

Upvotes: 11

Marek Sebera
Marek Sebera

Reputation: 40631

You can place non-public class in a file, and it's not a bug but feature.

Your problem is on level of packaging, not compile. Because you can compile this file with non-public class, but you can't call it from outside, so it's not working as application base class

Like this:

// [+] single file: SomeWrapper.java 

public class SomeWrapper {
    ArrayList<_PrivateDataType> pdt;
}
// [-] single file: SomeWrapper.java 

// [+] single file: _PrivateDataType.java 
class _PrivateDataType {
    // members, functions, whatever goes here
}

// [-] single file: _PrivateDataType.java 

Upvotes: 2

Related Questions