Are undeclared classes in Java public?

I am starting to learn Java and creating my first global hello function in Eclipse. I noticed that the following two functions, like in the default package in the src folder in my java project, seem to do the same:

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

and

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

Both successfully print "Hello World!" to the console.

I read a little about the different types of classes, but I'm not sure which type of class I will declare with the first function. What is the difference between these two functions? Is java for my greeting a world class in the first case public?

+5
source share
4 answers

Class, which does not declare itself as publicis package protected, which means that it Classis available only in this package.

acccess fooobar.com/questions/43/.... oracle

:

, , :

com
  stackoverflow
    pkg1
      public Class1
      Class2
    pkg2
      OtherClass

Class2 Class1, OtherClass

+10

2 , Google:

http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

In response to your question, the default modifier package protected, that is, it can only be obtained in the package, but next time, please do some research before posting the question, since it took you more time to write a question about what it should look for

+1
source

This is a protected class; you cannot access this side of the class package.

If you did not specify something of your own by default, protected

0
source

All Articles