Saturday, January 16, 2016

What is access modifier?

Access Modifier:


Access Modifier are those which change or modify the accessibility of any variable which gives additional meaning to data, methods and classes.

Final cannot be modified at any point of time.


Modifier Keywords: 

final, volatile, static, transient, abstract, native, synchronized.

Access modifier:
Access modifier specifies who can access them. There are 4 access modifiers used in Java. They are:
public, private, protected and no modifier.

Class level access modifier(java classes only):
2 access modifier is allowed. public and no modifier.

If a class is public, then it can be accessed from anywhere.
If a class has no modifier, then it can only be accessed from same packager.

Member level class modifiers(java variables and java methods):
All the 4 modifier is allowed.

Access Modifier
Same Class
Same Package
Sub Class
Other Packages
Public
Y
Y
Y
Y
Protected
Y
Y
Y
N
Default/No Modifier
Y
Y
N
N
Private
Y
N
N
N



Q: What is the difference between access specifiers and access modifiers?
Ans by Michael Bogwardt:
No. "access modifier" is the official term for private, protected and public, whereas the term "access specifier" does not occur at all in the Java language specification and is AFAIK not something that is widely used.

"access modifier" is the official term for private, protected and public used in the Java language specification. "access specifier" is used synonymously in the Java API doc, but this is the first time I've noticed that. It's probably better to stick with the JLS term.

Others: 
Java has basically 2 types of Modifiers:

  1. java access modifiers
  2. java non-access modifiers

Java access modifiers and Java access specifiers are the same thing, which are public, private, protected.

No comments:

Post a Comment