What is the default access modifier in Java?

What is the default access modifier for a method or instance variable unless I specify 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 flight.booking , call this constructor?

+85
java access-modifiers
Apr 23 '13 at 8:48
source share
13 answers

From Java documentation

If a class does not have a modifier (by default, also called package-private), it is visible only within its own package (packages are called groups of related classes - you will learn about them in a later lesson.)

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

The full story you can read here ( which I wrote recently ):

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

+83
Apr 23 '13 at 8:52
source share

From the documentation:

 Access Levels Modifier Class Package Subclass World ----------------------------------------------------- public YYYY protected YYYN (Default) YYNN private YNNN 
+50
Jul 14 '15 at 17:18
source share

It depends on the context.

When it is inside the class:

 class example1 { int a = 10; // This is package-private (visible within package) void method1() // This is package-private as well. { ----- } } 

When it is in the interface:

 interface example2 { int b = 10; // This is public and static. void method2(); // This is public and abstract } 
+25
Dec 23 '13 at 23:00
source share

The default access modifier is a closed package - visible from only one package

+20
Apr 23 '13 at 8:50
source share

Here is an example of code that should pretty much summarize this for you ... In addition to the below, showing how you cannot access the default in another package, there is one more thing.

By default, it is not available in a subclass if the class that subclasses it in another package, but is available 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 } } 
+7
Jan 02 '14 at 4:45
source share

The default modifier is package . Only code in the same package can call this constructor.

+5
Apr 23 '13 at 8:52
source share

The constructor access modifier will be package-private (by default) . Since you declared the class public, it will be visible everywhere, but the constructor will not. Your constructor will only be visible in its package.

 package flight.booking; public class FlightLog // Public access modifier { private SpecificFlight flight; FlightLog(SpecificFlight flight) // Default access modifier { this.flight = flight; } } 

If you are not writing any constructor in your class, then the compiler generates a default constructor with the same class access modifier. In the following example, the compiler will generate a default constructor with the public access modifier (the same as the class).

 package flight.booking; public class FlightLog // Public access modifier { private SpecificFlight flight; } 
+2
Apr 23 '13 at 9:03 on
source share

The default access modifier is package-private (i.e., DEFAULT), and it is displayed from only one package.

+1
Apr 23 '13 at 9:26
source share

Yes, it can be seen in the same package. Anything outside this package will not have access to it.

+1
Oct. 21 '15 at 11:13
source share

Is the access modifier protected or packaged by this constructor?

I think that implicitly your constructor access modifier will be your class access modifier. since your class has public access, the constructor will have public access implicitly

0
Apr 23 '13 at 8:50
source share

No, you cannot call the default access level to another package. But you have access in the package. For more information, follow this link .

0
Apr 21 '14 at 4:59
source share

Default access modifier - if a class does not have a modifier (by default, also called package-private), it is visible only within its own package (packages are called groups of related classes).

0
Apr 21 '14 at 5:38
source share

From a book called OCA Java SE 7 Programmer I:

Class members defined without using explicit access modifiers are defined using package accessibility (also called default accessibility). Members with access to the package are available only for classes and interfaces defined in one package.

0
Aug 17 '15 at 10:26
source share



All Articles