What is the default access modifier?

I just started reading a Java book and was surprised; which access modifier is the default if not specified?

+103
java access-specifier
Aug 20 '10 at 10:35
source share
12 answers

The default visibility is known as a “private package” (although you cannot use it explicitly), which means that the field will be accessible from within the same package to which the class belongs.

As mdma pointed out, this is not the case for interface members who use "public" by default.

See Java Access Qualifiers

+112
Aug 20 '10 at
source share

The default qualifier depends on the context.

For classes and interface declarations, a closed package is used by default. This happens between protected and private, which allows you to use only classes in the same package. (protected is similar to this, but also allows access to subclasses outside the package.)

class MyClass // package private { int field; // package private field void calc() { // package private method } } 

For interface members (fields and methods), the default access is public. But note that the default interface declaration uses the private package.

 interface MyInterface // package private { int field1; // static final public void method1(); // public abstract } 

If we have an ad

 public interface MyInterface2 extends MyInterface { } 

Classes using MyInterface2 can see fields1 and method1 from the superinterface because they are publicly available, although they cannot see the declaration of MyInterface itself.

+79
Aug 20 '10 at 10:50
source share

If an access specifier is not specified, this is access to the packet level (there is no explicit qualifier for this) for classes and class members. Interface methods are implicitly public.

+15
Aug 20 '10 at 10:39
source share

By default, the package is visible (without a keyword), which means that it will be available for each class that is in the same package.

It is interesting to note that protected does not restrict visibility for subclasses, as well as for other classes in one package

+9
Aug 20 '10 at 10:41
source share

It depends on what kind of thing it is.

  • Top-level types (that is, classes, enumerations, interfaces, and annotation types not declared inside another type) are by default closed to packages . ( JLS §6.6.1 )

  • In classes, all members (that is, fields, methods, and declarations of nested types) and constructors are by default closed to the package . ( JLS §6.6.1 )

    • When a class does not have an explicitly declared constructor, the compiler inserts a default constructor with a null argument that has the same access specifier as the class . ( JLS §8.8.9 ) The default constructor is usually distorted as always public, but in rare cases this is not equivalent.
  • In enumerations, Constructors are private by default. Indeed, enumerators must be private and mistakenly designate them as public or protected. The enum constants are always public and do not allow any access specifier. Other members of enumerations are closed to the package by default. ( JLS §8.9 )

  • In interfaces and annotation types, all members (again, this means that fields, methods, and declarations of nested types) are open by default. Indeed, interface members and annotation types must be public, and mistakenly designate them as private or protected. ( JLS §9.3–9.5 )

  • Local classes are named classes declared inside a method, constructor, or initializer block. They are limited to the area { .. } in which they are declared, and do not allow any access specifier. ( JLS §14.3 ) Using reflection, you can instantiate local classes from other places, and they are private packages , although I'm not sure if this detail is in JLS.

  • Anonymous classes are user classes created with new that specify the body of the class directly in the expression. ( JLS §15.9.5 ) Their syntax does not allow any access specifier. Using reflection, you can instantiate anonymous classes from other sources, and they, and their generated constructors, are private packages , although I'm not sure if this detail exists in JLS.

  • Instance and static initializer blocks do not have language level access specifiers ( JLS §8.6 and 8.7 ), but static initializer blocks are implemented as a method named <clinit> ( JVMS §2.9 ), so the method must internally have some access qualifier. I studied the classes compiled by the javac and Eclipse compiler using a hex editor, and found that both generate the method as a batch-private one . However, you cannot call <clinit>() in the language, because the < and > characters are not allowed in the method name, and reflection methods strongly deny its existence, so in fact its access specifier does not have access . A method can only be called by a virtual machine during class initialization. Instance initializer blocks are not compiled as separate methods; their code is copied to each constructor, so they cannot be accessed individually, even by reflection.

+8
Oct 21 '15 at 2:05
source share

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 available from its own class or package, it will also be set if the access modifier is missing.

  Access Levels Modifier Class Package Subclass EveryWhere public YYYY protected YYYN default YYNN private YNNN 

if you use the default value in the interface, you can implement a method like this example

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

However, this will only work with version 8 of Java

Official documentation

Access Modifiers in Java

+4
Sep 05 '18 at 5:44
source share

See here for more details. By default, none of the private / public / secure is used, but a completely different access specification. It is not widely used, and I prefer to be more specific in my access definitions.

+3
Aug 20 '10 at 10:40
source share

The default access specifier is package . Classes can access members of other classes in the same package. But outside the package, it looks like closed

+3
Jul 19 '12 at 10:37
source share

Here's a quote about package-level visibility from an interview with James Gosling, the creator of Java:

Bill Venners: Java has four access levels. By default, the package is used. I always wondered if setting access to the package by default is convenient because the three keywords that C ++ people already knew about were private, protected, and public. Or if you have a reason why you thought access to the package should be the default.

James Gosling: A package is usually a collection of things that are written together. All in all, I could do one of two things. One of them was that you always put the keyword that the domain gives you. Or I could have a default value. And then the question arises: what makes a reasonable default? And I tend to be the least dangerous thing.

Thus, the public would be very bad to default. Private would probably be a bad thing by default, if only because people actually don't often write private methods. And the same with protected. And looking at the bunch of code that I had, I decided that the most common thing that was safe enough was in the package. And C ++ did not have a keyword for this, because they did not have the concept of packages.

But I liked it, not friends, because with friends you should list who all your friends are, and so if you add a new class to the package, then usually you have to go to all the classes of this package and update their friends, which I always considered a complete pain in the butt.

But the friend list alone causes a version issue. And so it was the concept of a friendly class. And the good thing is that I did this by default - will I solve the problem so that the keyword be?

For a while, there was actually a friendly keyword. But since all others start with "P," it was "friendly" with "PH." But it was only there, maybe a day.

http://www.artima.com/intv/gosling2P.html

+3
Jan 14 '16 at 4:16
source share

Update Java 8 using the default keyword: Like many others noted default visibility (without the keyword)

the field will be accessible from within the same package to which the class belongs.

Not to be confused with the new Java 8 feature ( Default Methods ), which allows the interface to provide when it is marked with the default keyword.

See: Access Modifiers

+2
Aug 05 '16 at 16:52
source share

In JAVA, there is an access modifier called "default", which allows the direct creation of an instance of this object only inside this package.

Here is a useful link:

Java Modifiers / Specifications

+1
Sep 21 '17 at 7:44 on
source share

First of all, let me say one thing: in java there is no such term as "Access Specifier". We should call everything "modifiers." Since we know that final, static, synchronized, volatile .... are called modifiers, even Public, private, protected, default, abstract should also be called modifiers. By default, these are modifiers where there is no physical existence, but modifiers are not placed, so it should be considered as default modifiers.

To justify this, take one example:

 public class Simple{     public static void main(String args[]){      System.out.println("Hello Java");     } } 

The output will be: Hello Java

Now change the public to private and see what compiler error you get: It says: “The private modifier is not allowed here” What is the conclusion: someone may be wrong, or some tutorial may be wrong, but the compiler cannot be wrong. Thus, we can say that the terminal access specifier in java is not a modifier.

-2
Aug 22 '17 at 8:03 on
source share



All Articles