Java Modifiers
As of now, in this tutorial we have seen only public keyword in most of the examples. The public keyword is an access modifier, which is used to set the access level of classes, attributes, methods and constructors.
There are two types of modifiers in Java such as:
Access Modifiers – It controls the access level in the program.
Non -Access Modifiers – It does not controls the access levels but has other functionalities to perform in the program.
Access Modifiers
In Java, for classes we use either public or default.
Modifier |
Description |
public |
The class is accessible
by other classes |
default |
The class is only
accessible by other classes in the same package. This is used when we do not
specify a modifier in the class |
For attributes, methods and constructors, we can use any one of the following:
Modifier |
Description |
public |
The code is accessible
for all the classes |
private |
The code is only
accessible within the declared class |
protected |
The code is accessible
in the same package and its subclasses. |
default |
The code is only
accessible in the same package. This is used when we do not specify the
modifier. |
Non-Access Modifiers
For classes, we can use either final or abstract:
Modifier |
Description |
final |
The class cannot be
inherited by other classes |
abstract |
The class cannot be used
to create the objects. The abstract class contains abstract methods |
For attributes and
methods, we can use any one of the following:
Modifier |
Description |
final |
These attributes /
methods cannot be override or modified |
abstract |
Abstract methods can
only be used in the abstract class. Only methods can be abstract. The method
can have a body only in the sub classes which is known as inheritance |
static |
Attributes / methods
belong to the class, rather than an object |
transient |
Attributes / methods are
skipped while serializing the object containing them |
synchronized |
Methods can only be accessed by one thread at a time |
volatile |
The value of an
attribute is not cached by the thread-locally and it is always read from the
main-memory |
Final
If you don’t want to override the class attributes, we use the final keyword while declaring the class attribute.
Example
In the below example, we declared the attribute with final keyword, hence when we try to modify the value of ‘x’, it throws error:
public class MyClass { final int x = 100; public static void main(String args[]){ MyClass myObj = new MyClass(); myObj.x = 50; System.out.println(myObj.x); } }
Error
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The final field MyClass.x cannot be assigned
The final keyword is always used in the constant variable (Example: pi (3.14)).
Static
The static method can be accessed without creating an object for a class, unlike public.
Example
In the following example, we could see the difference between the static and public methods:
public class StaticvsPublic { static void staticMethod(){ System.out.println("Static methods can be called without creating objects"); } public void publicMethod(){ System.out.println("Public methods must be called by creating objects"); } public static void main(String args[]){ staticMethod (); StaticvsPublic myObj = new StaticvsPublic(); myObj.publicMethod(); } }
Output
Static methods can be called without creating objects Public methods must be called by creating objects
The abstract method belongs to the abstract class which does not have a body. The body for the abstract method is provided by the sub class.
Example
Code from file
AbstractMethod.java
abstract class AbstractMethod { String institute = "SparkDatabox"; int total_courses = 500; public abstract void enroll(); }
Code from file Training.java
public class Training extends AbstractMethod { String course = "Java Training"; public void enroll(){ System.out.println("You have been enrolled for Online Training"); } }
Code from file MyMainClass.java
public class MyMainClass { public static void main(String args[]){ Training training = new Training(); System.out.println("Institute : "+training.institute); System.out.println("Total no. of courses : "+training.total_courses); System.out.println("Course Enrolled : "+training.course); training.enroll(); } }
Output
Institute : SparkDatabox Total no. of courses : 500 Course Enrolled : Java Training You have been enrolled for Online Training
Here the abstract method enroll has been defined the class Training which extends the abstract class.