By now, you’re likely well-acquainted with the public keyword, which is frequently featured in our examples.
public class Main |
The public keyword serves as an access modifier, determining the access level for classes, attributes, methods, and constructors.
Modifiers are categorized into two groups:
Regarding classes, you have the option to use either public or default access modifiers:
Modifier |
Description |
public |
The class can be accessed by any other class. |
default |
The class is solely accessible from classes within the same package. This is the default behavior when no modifier is specified. Further details on packages will be covered in the Packages chapter. |
Attributes, methods, and constructors can be specified using one of the following access modifiers:
Modifier |
Description |
public |
The code can be accessed by all classes. |
private |
The code is exclusively accessible within the declared class. |
default |
The code is solely accessible within the same package. This is the default behavior when no modifier is specified. Further details on packages will be covered in the Packages chapter. |
protected |
The code is accessible within the same package and subclasses. Further details on subclasses and superclasses will be covered in the Inheritance chapter. |
Regarding classes, you have the option to use either final or abstract modifiers:
Modifier |
Description |
final |
The class cannot be extended by other classes. Further information on inheritance will be provided in the Inheritance chapter. |
abstract |
The class cannot be instantiated. To utilize an abstract class, it must be extended by another class. Further details on inheritance and abstraction will be covered in the Inheritance and Abstraction chapters. |
Access Modifiers – governing the access lev
Attributes and methods can be specified using one of the following modifiers:
Modifier |
Description |
final |
Attributes and methods are immutable; they cannot be overridden or modified. |
static |
Attributes and methods are associated with the class itself, rather than individual objects. |
abstract |
This modifier is exclusively applicable within an abstract class and is limited to methods. It signifies that the method lacks an implementation, as exemplified by abstract void run();. The implementation is provided by the subclass through inheritance. Further insights on inheritance and abstraction will be provided in the respective chapters. |
transient |
Attributes and methods marked with this modifier are omitted during the serialization process of the containing object. |
synchronized |
Methods synchronized restrict access to only one thread at a time. |
volatile |
The value of a volatile attribute is consistently read from the “main memory” and not cached locally to any thread. |
To prevent the overriding of existing attribute values, declare attributes as final :
|
A static method can be accessed without the need to create an object of the class, unlike public methods :
An example illustrating the distinctions between static and public methods:
|
An abstract method is associated with an abstract class and lacks a body. The implementation for the method is provided by the subclass.
|