134

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?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
yrazlik
  • 9,023
  • 26
  • 84
  • 145
  • 2
    It is already covered in [this old question](http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private?rq=1) look for "no modifier" and "default" – default locale Apr 23 '13 at 08:49
  • 7
    I googled for "java default access modifier" and the first result was [Controlling Access to Members of a Class](http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html) from Oracle. A simple search... – maba Apr 23 '13 at 08:50
  • 6
    thank you, i searched but i could not find the same question. No need fordownvote – yrazlik Apr 23 '13 at 08:52
  • 11
    SO is far more straightforward for specific questions than official documentation, so I'll click a prominent SO search result before trying a page from a manual. I appreciate strictly unnecessary questions like this. – Grault Dec 13 '13 at 04:59
  • Yes. Other classes can use the constructor. See this [cheat sheet](http://stackoverflow.com/a/33627846/276052). – aioobe Nov 10 '15 at 15:54
  • Update **Java 8** usage of `default` keyword: As many others have noted The default visibility (no keyword) > the field will be accessible from inside the same package to which the > class belongs. Not to be confused with the new **Java 8** feature ([Default Methods][1]) that allows an interface to provide an implementation when its labeled with the `default` keyword. See: [Access modifiers][2] [1]: https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html [2]: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html – Ahmad Sanie Aug 05 '16 at 16:53

13 Answers13

125

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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Suresh Atta
  • 114,879
  • 36
  • 179
  • 284
111

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
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Pradeep
  • 5,421
  • 8
  • 31
  • 58
  • 7
    Default seems slightly out of place since a subclass could reference something with the "default modifier" (no) provided the inheritance occurs within the same package. – deadboy Jul 15 '16 at 02:03
  • 1
    what does world significate? – ziMtyth Mar 19 '18 at 15:08
  • @EvinUgur I think it makes sense, you can have, for example, a class _Canine_, which has a default method _eatHumans()_, Wolves extend from that, but then you have a package _pets_ somewhere, which contains the class _Dog_ that also extends from Canine, but you don't want it to eatHumans. – Viorel May 01 '18 at 07:07
  • 2
    @ziMtyth World, as in "Global", accessible from anywhere we have an instance of that class. – Viorel May 01 '18 at 07:07
39

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
}
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
SKG
  • 634
  • 6
  • 6
21

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

Evgeniy Dorofeev
  • 124,221
  • 27
  • 187
  • 258
  • why you write visible from the same package, you means if we write class A { int a=0; }, so it should be accessible from other class with the package ?? – Abdul Rahman Oct 20 '15 at 06:21
15

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
    }
}
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
JTHouseCat
  • 435
  • 4
  • 4
  • 2
    I gave you +1 but then I noticed one mistake in your explanation. It says "Default is non accessible in sub-classes and acts like a private (and doesn't act like a protected or public when it comes to a subclass)." which not true. In case of subclass, if it is in same package it can access members with default access modifier while if it is in another package then it can't. So please correct it!!! Remember: 'default' access modifier is package-private which means every class (whether extending it or not) within same package can access it. – sactiw Apr 23 '14 at 05:54
  • Excellent find. I modified the wording. – JTHouseCat May 05 '14 at 20:54
  • Thx, this example – HenryChuang Nov 15 '19 at 13:13
5

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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
cahen
  • 11,836
  • 12
  • 39
  • 68
5

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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Maninder Singh
  • 115
  • 4
  • 12
3

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

Piyush Bhardwaj
  • 636
  • 5
  • 20
2

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;
}
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Sachin Gorade
  • 1,309
  • 10
  • 29
1

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Hariprasath
  • 798
  • 4
  • 11
  • 38
1

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

Atiq
  • 158
  • 2
  • 6
  • 20
0

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

PermGenError
  • 43,905
  • 8
  • 81
  • 104
0

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
BERGUIGA Mohamed Amine
  • 5,202
  • 3
  • 34
  • 32