What is the default access specifier in Java?

Viewed 162415

I just started reading a Java book and wondered; which access specifier is the default one, if none is specified?

12 Answers

default is a keyword that is used as an access modifier for methods and variables.
Using this access modifier will make your class, variable, method or constructor acessible from own class or package, it will be also is set if no access modifier is present.

  Access Levels
    Modifier    Class   Package Subclass  EveryWhere
    public        Y        Y       Y         Y
    protected     Y        Y       Y         N
    default       Y        Y       N         N
    private       Y        N       N         N

if you use a default in a interface you will be able to implement a method there like this exemple

public interface Computer {    
    default void Start() {
        throw new UnsupportedOperationException("Error");
    }    
}

However it will only works from the 8 Java version

Official Documentation

Access Modifiers in Java

There is an access modifier called "default" in JAVA, which allows direct instance creation of that entity only within that package.

Here is a useful link:

Java Access Modifiers/Specifiers

Related