[java] What is the default access modifier in Java?

What is the default access modifier for a method or an instance variable if I do not state it explicitly?

For example:

package flight.booking;

public class FlightLog
{
    private SpecificFlight flight;

    FlightLog(SpecificFlight flight)
    {
        this.flight = flight;
    }
}

Is the access modifier of this constructor protected or package? Can other classes in the same package, which is flight.booking, call this constructor?

This question is related to java access-modifiers

The answer is


The default modifier is package. Only code in the same package will be able to invoke this constructor.


It depends on the context.

When it's within a class:

class example1 {

    int a = 10; // This is package-private (visible within package)

    void method1() // This is package-private as well.
    {
        -----
    }
}

When it's within a interface:

interface example2 {

    int b = 10; // This is public and static.
    void method2(); // This is public and abstract
}

Is the access modifier of this constructor protected or package?

I think implicitly your constructors access modifier would be your class's access modifier. as your class has public access, constructor would have public access implicitly


Here is a code sample which should pretty much sum this up for you... In addition to the below, showing how you can't access a default in another package there is one more thing.

Default is not accessible in a subclass if the class that subclasses it is in another package, but it is accessible if the subclass is in the same package.

package main;

public class ClassA {
    private int privateVar;
    public int publicVar;
    int defaultVar;
}

package main;

public class ClassB {
    public static void main(String[] args) {
        ClassA a = new ClassA();
        int v1 = a.publicVar;   // Works
        int v2 = a.defaultVar;  // Works
        int v3 = a.privateVar;  // Doesn't work

    }
}

package other;

public class ClassC {
    public static void main(String[] args) {
        ClassA a = new ClassA();
        int v1 = a.publicVar;   // Works
        int v2 = a.defaultVar;  // Doesn't work
        int v3 = a.privateVar;  // Doesn't work
    }
}

Default access modifier is package-private - visible only from the same package


No, you can't call the default access level to the other package. But you have the access within the package. Follow this link for more details.


From documentation:

Access Levels
Modifier        Class    Package    Subclass    World
-----------------------------------------------------
public           Y        Y          Y           Y
protected        Y        Y          Y           N
(Default)        Y        Y          N           N
private          Y        N          N           N

From a book named OCA Java SE 7 Programmer I:

The members of a class defined without using any explicit access modifier are defined with package accessibility (also called default accessibility). The members with package access are only accessible to classes and interfaces defined in the same package.


Yes, it is visible in the same package. Anything outside that package will not be allowed to access it.


Default access modifier - If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes).


The Default access modifier is package-private (i.e DEFAULT) and it is visible only from the same package.


Your constructor's access modifier would be package-private(default). As you have declared the class public, it will be visible everywhere, but the constructor will not. Your constructor will be visible only in its package.

package flight.booking;

public class FlightLog // Public access modifier
{
    private SpecificFlight flight;

    FlightLog(SpecificFlight flight) // Default access modifier
    {
        this.flight = flight;
    }
}

When you do not write any constructor in your class then the compiler generates a default constructor with the same access modifier of the class. For the following example, the compiler will generate a default constructor with the public access modifier (same as class).

package flight.booking;

public class FlightLog // Public access modifier
{
    private SpecificFlight flight;
}