What is an example of access modifiers



Java offers modifiers for many areas. These can be used to mark classes, attributes and / or methods for various purposes.

The following modifiers are available: public, private, protected, static, final, abstract, transient, volatile, synchronized andnative. The most well-known modifiers among them are probably the access modifiers public, private and protected which is used to control the visibility of the class, the attribute or the method.

The following overview shows the areas for which the individual modifiers are used:

abstract   x   x x
final x x   x  
native   x      
private x x x    
protected x x x    
public x x x x x
static x x      
synchronized   x      
transient x        
volatile x        



The modifier abstract can be used with a method, class or interface. This means that with abstract marked element has not yet been fully implemented. Is a method or an interface with the moficator abstract the implementing class must also be provided as a abstract be declared. When a class with abstract is marked, no object can be created from it.

A subclass derived from an abstract class must fully implement all abstract methods of the parent class, otherwise it must also be used as abstract be declared. In contrast to an interface, an abstract class can also have implemented methods and attributes (which are not static or final are declared). If there is no implementation in an abstract class, it makes more sense to declare it as an interface.


publicabstractclass point {int x_coordinate; int y_coordinate; abstractvoid draw ();}

The Point class contains the abstract method draw. This method is with abstract and therefore does not have a method body. The method head is ended directly with a semicolon. By doing that method draw is abstract, the point class must be abstract as well.



The modifier final can be used for attributes, methods and classes. Attributes with final are constants and can no longer be changed. Methods that have the final modifier cannot be overwritten when inherited. A class, on the other hand, is at the end of an inheritance hierarchy if you use final has been declared. No further subclasses can be derived from it.


// Constant PI can no longer be changed final double PI = 3.14;

The above example shows the use of final. In this case it is a constant that can no longer be changed.



Only methods can use the modifier native be marked. These methods are written in a different programming language and are used for "multilingual" communication. There native If it is used more in the professional field, we will forego an example at this point.



The access modifier private can be used with attributes, methods and constructors. These cannot then be seen and used by another class (not even by derived classes). For better data encapsulation, attributes should be given this modifier as far as possible.


publicabstractclass point {/ * Attributes were declared with private, so direct access to them from outside is not possible * / privateint x_koordinate; privateint y_koordinate; // Returns the x-position of our point publicint getX_koordinate () {return x_koordinate;} / * Other classes can only change the X-coordinate via the set method, since the x_coordinate attribute is private and therefore does not allow direct access from outside. The method setX_koordinate, on the other hand, is public, so that it can be accessed from outside and the coordinate within the method can be changed. The new value of the coordinate is transferred as a parameter. In this way, the attribute itself remains hidden from the outside world. * / publicvoid setX_coordinate (int x_coordinate) {this.x_coordinate = x_coordinate;} // Returns the y position of our point publicint getY_coordinate () {return y_coordinate;} / * see setX_coordinate * / publicvoid setY_coordinate (int y_coordinate) {this.y_coordinate) {this = y_coordinate;}}



The access modifier protected can just like private can be used, but here the visibility is limited to the package and not as with private only in their own class. Subclasses could be with protected also always "see" declared elements. So in the example above, you would private by protected replace, then the attributes and methods would be usable within the package in which the class Punkt is located and all subclasses derived from Punkt could access them directly.



The access modifier public is allowed for all constructs. The elements declared can be used by any class, so they are public. Attributes of your own class that start with private marked, read out and changed (see example from private).



With the modifier static class variables or class methods are identified.

Class methods are independent of objects. The static method is therefore called via the class name and not via the object name or this pointer.

In contrast to attributes, class variables have the same value for every object in the class. To a certain extent, the instances of the class share the variable. If the class variable in an object is changed, all other objects have also received the change.


publicabstractclass point {privateint x_koordinate; privateint y_koordinate; staticint pointCounter; // number of all objects of the point class}

In the above example we have built in the class variable PunkteZähler. The modification of the variable PunkteCounter affects all objects. For example, if it is increased by one in the class constructor, it is increased by one for each point object, i.e. the value is the same for all objects.



With the modifier synchronized only methods are marked. This ensures data consistency when using threads if one thread wants to read and another thread wants to have write access to data. Threads are explained in more detail in a later chapter.


/ * Threads (processes) can only access this method one after the other and not at the same time. * / publicsynchronizedvoid setY_koordinate (int y_koordinate) {this.y_koordinate = y_koordinate;}



Attributes that start with the modifier transient are not included in a serialization, which means, for example, that these attributes would not be written to a file.

/ * This variable would not be saved to a file * / privatetransientint y_koordinate;



Attributes that start with the modifier volatile allow simultaneous access by two or more threads.

/ * This variable can be accessed by several threads (processes) at the same time. * / privatevolatileint y_coordinate;