Why do we use access specifiers

What is the standard access specifier in Java?

I just started reading a Java book and was wondering; If one is not specified, which access specifier is the default specifier?


The default visibility is called "package-private" (although you can't use this explicitly). This means that the field can be accessed within the same packet to which the class belongs.

However, as mdma pointed out, this does not apply to interface members for which the default value is "public".

See Java's access specifier

The default identifier depends on the context.

For classes and interface declarations, the default value is package private. This is somewhere between protected and private and only allows classes in the same package access. (protected is like this, but also allows access to subclasses outside of the package.)

Standard access is public for interface members (fields and methods). Note, however, that the interface declaration itself is by default the package private.

Then when we have the explanation

Classes using MyInterface2 can then display Field1 and Method1 through the superinterface because they are public even though they cannot see the declaration of MyInterface itself.

If no access specifier is specified, access is at the package level (there is no explicit specifier for it) for classes and class members. Interface methods are implicitly public.

The default visibility (not a keyword) is package. This means that it is available for any class that is in the same package.

What is interesting is that protected the visibility is not limited to the subclasses, but also to the other classes in the same package

It depends what that thing is.

  • Top-level types (i.e. classes, enumerations, interfaces, and annotation types not declared within another type) are Package-private By default. (JLS §6.6.1)

  • In the classes, all members (i.e. fields, methods, and nested type declarations) and constructors are Package-private By default. (JLS §6.6.1)

    • If a class does not have an explicitly declared constructor, the compiler inserts a default constructor with null arguments, the has the same access specifier as the class . (JLS §8.8.9) The default constructor is often given incorrectly because it is always public. However, in rare cases this is not equivalent.
  • In enumerations, constructors are standard Private . Indeed, Enum contructors got to be private, and specifying them as public or protected is a mistake. Enum constants are always public and do not allow an access specifier. There are other members of bullet lists Package-private By default. (JLS §8.9)

  • In interfaces and annotation types, all elements (again fields, methods and nested type declarations) are standard public . Actually members of interfaces and annotation types have to be public, and specifying them as private or protected is a mistake. (JLS §9.3 to 9.5)

  • Local classes are named classes that are declared in a method, constructor, or initialization block. you are limited to the block .. in which they are declared, and do not allow an access specifier. (JLS §14.3) Using reflection, you can instantiate from other local classes, and they are Package-private although I'm not sure the detail is in the JLS.

  • Anonymous classes are user-defined classes that are used to specify a class body directly in the expression. (JLS §15.9.5) Their syntax does not allow an access specifier. Using Reflection, you can instantiate anonymous classes from elsewhere, and both they and their generated constructors are packet private although I'm not sure if this detail is in the JLS.

  • Instance and static initialization blocks have no language level access specifiers (JLS §8.6 and 8.7), but static initialization blocks are implemented as a method named (JVMS §2.9), so the method must have an access specifier internally. I've examined classes compiled by javac and Eclipse using a hex editor and found that both of them use the method as package-private to generate . However, you cannot invoke within the language because the characters and in a method name are invalid and the reflection methods are hardwired to deny their existence. Hence, this is effectively the access specifier Access specifier no access . The method can only be called by the VM during class initialization.Instance initialization blocks are not compiled as separate methods. Your code is copied into each constructor so that they cannot be accessed individually, even by reflection.

default is a keyword used as an access modifier for methods and variables.
Using this access modifier makes your class, variable, method, or constructor accessible from its own class or package. It is also set if there is no access modifier.

If you use a default setting in an interface, you can implement a method like this there

However, it only works from the 8 Java version

Official documentation

Access modifiers in Java

See here for more details. The default value is not private / public / protected, but a completely different access specification. It's not widely used and I prefer to be much more specific in my access definitions.

The default access specifier is package. Classes can access the members of other classes in the same package. However, outside of the package, it is saved as a Private displayed

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

Bill Venners : Java has four levels of access. The default is package. I've always wondered if it was practical to make package access the default, since the three keywords C ++ already knew were private, protected, and public. Or if you had a specific reason that you thought package access should be the default.

James Gosling : A package generally consists of a number of things written together. Generically, I could have done one of two things. One of them was that you always had to enter a keyword that the domain gives you. Or I could have had a default value. And then the question is what makes a reasonable standard? And I tend to do what is the least dangerous.

So the public would have been a really bad thing to set the standard. Private would probably have been a bad thing to set a standard, if only because people don't write private methods that often. And the same with protected. And when I looked at a lot of code that I had, I decided that the package was the most common, which was reasonably safe. And C ++ didn't have a keyword for it because they had no idea of ​​packages.

But I liked it more than the idea of ​​friends because with friends you kind of have to enumerate who all of your friends are. So generally when you add a new class to a package you have to go to all of the classes in that package and update their friends which has always been a total pain in my ass.

But the friends list itself is causing some kind of version problem. And so there was the idea of ​​a friendly class. And the nice thing that I made this the standard - I'll solve the problem. What should the keyword be?

For a while there was actually a friendly keyword. But because everyone else starts with a "P", it was phriendly with a "PH". But that was only there for a day maybe.


Update the usage of the keyword in Java 8 : As many others have noted The default visibility (no keyword)

The field can be accessed from the same package that the class belongs to.

Not to be confused with the new one Java 8- Function (standard methods) with which an interface can provide an implementation when it is marked with the keyword.

See: Access Modifiers

First, let me say that there is no such thing as "access specifier" in Java. We should refer to everything as "modifiers". Since we know that final, static, synchronized, ephemeral ... are called modifiers, public, private, protected, standard, abstract should also be called modifiers. Standard are those modifiers where there is no physical existence but no modifiers are placed. Then they should be treated as standard modifiers.

To justify this, take an example:

Output will be:

Now change public to private and see what compiler error you get: It says "Modifier private is not allowed here". The bottom line is that someone can be wrong, or that a tutorial can be wrong, but the compiler cannot be wrong. So we can say that there is no term access specifier in Java. Everything are modifiers.

We use cookies and other tracking technologies to improve your browsing experience on our website, to show you personalized content and targeted ads, to analyze our website traffic, and to understand where our visitors are coming from.

By continuing, you consent to our use of cookies and other tracking technologies and affirm you're at least 16 years old or have consent from a parent or guardian.

You can read details in our Cookie policy and Privacy policy.