What is the default access modifier in Java?

JavaAccess Modifiers

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

From Java documentation

> 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 — you will learn about them in a later lesson.)

At the member level, you can also use the public modifier or no modifier (package-private) just as with top-level classes, and with the same meaning.

Full story you can read here (Which I wrote recently):

http://codeinventions.blogspot.com/2014/09/default-access-modifier-in-java-or-no.html

Solution 2 - Java

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

Solution 3 - Java

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
}

Solution 4 - Java

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

Solution 5 - Java

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
    }
}

Solution 6 - Java

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

Solution 7 - Java

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

Solution 8 - Java

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

Solution 9 - Java

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;
}

Solution 10 - Java

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.

Solution 11 - Java

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).

Solution 12 - Java

> 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

Solution 13 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionyrazlikView Question on Stackoverflow
Solution 1 - JavaSuresh AttaView Answer on Stackoverflow
Solution 2 - JavaPradeepView Answer on Stackoverflow
Solution 3 - JavaSKGView Answer on Stackoverflow
Solution 4 - JavaEvgeniy DorofeevView Answer on Stackoverflow
Solution 5 - JavaJTHouseCatView Answer on Stackoverflow
Solution 6 - JavacahenView Answer on Stackoverflow
Solution 7 - JavaManinder SinghView Answer on Stackoverflow
Solution 8 - JavaPiyush BhardwajView Answer on Stackoverflow
Solution 9 - JavaSachin GoradeView Answer on Stackoverflow
Solution 10 - JavaHariprasathView Answer on Stackoverflow
Solution 11 - JavaAtiqView Answer on Stackoverflow
Solution 12 - JavaPermGenErrorView Answer on Stackoverflow
Solution 13 - JavaBERGUIGA Mohamed AmineView Answer on Stackoverflow