- access modifiers: public, protected and private and default
- non-access modifiers: final, abstract, strictfp, ...
Access modifiers
There are 3 access modifiers which allow to define the access controls (or level of access). However, there is a fourth access control which is used if you do not specify any access modifier: it is called the default or package access.
Class access
A class can be declared with only 2 access controls: public and default:
- a class with the default level access can only be seen by classes within the same package.
- a class with the public level access can be seen by any classes in the Java Universe.
Class member access
A class member is either a class variable (instance variable or a static variable) or a method:
- Public member: they can be accessed by any other classes.
- Default member: they can be accessed only by a class or sub-class which is in the same package.
- Protected member: they can be accessed by any class or sub-class which is in the same package. They can only be accessed by sub-classes located in different package.
- Private member: they can be accessed by the class in which they are declared; they are not visible to any other classes or subclasses.
Visibility
|
Public
|
Default
|
Protected
|
Private
|
From the same class
|
Yes
|
Yes
|
Yes
|
Yes
|
From a class in the same package
|
Yes
|
Yes
|
Yes
|
No
|
From a subclass in the same package
|
Yes
|
Yes
|
Yes
|
No
|
From a subclass outside the same package
|
Yes
|
No
|
Yes
|
No
|
From a class outside a package
|
Yes
|
No
|
No
|
No
|
Local variable
A local variable can not be declared with an access modifier.
Non Access modifiers
final
- a final class cannot be subclassed,
- a final method cannot be overridden,
- a final primitive variable's value can not be modified (an instance variable, method's parameter or a local variable),
- a final reference variable can only refer to the same object during its lifetime; but the object's state can be modified.
- its value can only be declared from the declaration line or constructor
abstract
If a class is marked abstract then it can not be instantiated. An abstract class' purpose is to be extended by a subclass. Note that an abstract class may not have any abstract method.
If a method is marked as abstract then it has to be implemented by a concrete subclass. Therefore an abstract method can not be marked as "private" or "final" or "static". It can be marked as "public" or "protected" or nothing (="default"). An abstract method must end with a semicolon as follows:
public abstract void calculInterest(float loan); protected abstract float getInterestRate(); abstract void calculInterest(float loan);Note that if a class contains one abstract method then it has to be marked as an abstract class. Also, if an abstract class implements an interface, it does not require to implements the methods of this interface.
Transient
If a class is serializable, the compiler comes to the conclusion that the entire class' state is serializable. Declare an instance variable as "transient" if you do not want its value to be serialized.
Synchronized
A method declared with the "synchronized" modifier can only be accessed by one thread at a time. Note that you can combine it with any access modifier.
public synchronized int getSomething(){ return 0; }
Volatile
When you declare a variable with the "volatile" keyword, the JVM must make sure that all threads accessing the variable have their copies updated whenever the variable is modified.
strictfp
Making a class or a method 'strictfp' means that it should conform to the IEE754 standard rules for the floating point. Without this modifier, this rule may be platform dependent.
Native
A "native" method is intended to be implemented in platform dependant code like, for example, "C". Like an abstract method, it does not contain any implementation and end with a semicolon.
public native void printText ();
You can see an example here.
This table describes the modifiers which are allowed for class, methods and local or instance or static variables:
Class
|
Methods
|
Variables (non local)
|
Local variables
|
public
final
abstract
strictfp
|
public
protected
private
final
abstract
strictfp
static
synchonized
native
|
public
protected
private
final
static
transient
volatile
|
final
|