Modifiers In Java
There will be a number of Java modifiers covered in order to meet the needs of learners more efficiently.
Java Modifiers
When it comes to Java modifiers, you’re probably quite familiar with the public keyword that appears in most examples:
public class Main
Java modifiers such as public are access modifiers that control access to classes, attributes, methods and constructors.
Modifiers can be divided into two categories:
- Access Modifiers – controls the access level
- Non-Access Modifiers – provides other functionality instead of controlling access level
Access Modifiers In Java
The Java class modifiers public and default can be used for classes:
Modifier | Overview | Try it |
---|---|---|
public | Access to the class is possible from any other class. | Try it |
default | Classes in the same package can only access the class. In the absence of a modifier, this will be used. The Packages chapter contains more information about packages. | Try it |
In the case of attributes, methods, and constructors, choose one of the following:
Modifier | Overview | Try it |
---|---|---|
public | All classes have access to the code. | Try it |
private | A declared class is the only place where the code can be accessed. | Try it |
default | Access to the code is limited to the same package. In the absence of a modifier, this is used. | Try it |
protected | In the same package and subclasses, the code is accessible. The chapter on inheritance discusses subclasses and superclasses in more detail. | Try it |
Below is a brief table that explains Java’s access modifiers functionality.
Access Modifier’s | Within Class | Within Package | Outside Package (subclass only) | Outside Package |
---|---|---|---|---|
Private | Y | N | N | N |
Default | Y | Y | N | N |
Protected | Y | Y | Y | N |
Public | Y | Y | Y | Y |
Non-Access Modifiers
You can use either an abstract or a final modifier for classes:
Modifier | Overview | Try it |
---|---|---|
final | A class can’t be inherited by another class if it contains the final modifier. | Try it |
abstract | Objects cannot be created using the class with an abstract modifier (To access an abstract class, it must be inherited from another class). | Try it |
You can use one of the following attributes or methods:
Modifier | Overview |
---|---|
final | It is not possible to override or modify attributes and methods. |
static | Rather than belonging to an object, attributes and methods belong to a class. |
abstract | Usable only on abstract classes, and can only be implemented on methods. There is no body to the method, for example abstract void read();. Subclasses (the inherited class) provide the body. |
transient | When serializing an object, attributes and methods are skipped |
synchronized | There can only be one thread accessing a method at a time. |
volatile | An attribute’s value is not cached thread-locally, but instead is read from the “main memory.” |
Final
When it comes to Java modifiers, declare attributes as final if you don’t want them to override existing values:
Example:
Example:
Static
If it comes to Java modifiers, a static method can be invoked without initializing an object of the class.
To illustrate the difference between static and public methods, here is an example:
Example:
Example:
Abstract
When it comes to Java modifiers, abstract methods belong to abstract classes and do not have bodies.
Subclasses provide the body:
Example:
Example: