Lesson 15
Modifiers
Modifiers are special keywords that control accessibility, behavior and properties of classes, variables, methods and constructors in Java.
Java Track
Save progress as you learn.
61 lessons
Lesson content
Modifiers in Java are special keywords used to change the behavior, accessibility and properties of classes, variables, methods and constructors.
What are Modifiers in Java?
Modifiers are predefined keywords in Java that modify the default characteristics of classes, methods, variables and constructors.
- Control accessibility
- Restrict inheritance
- Improve security
- Support multithreading
- Define class and method behavior
Definition List
- Modifier: A keyword used to change the behavior or accessibility of a class, method, variable, or constructor.
- Access Modifier: A modifier that controls the visibility and accessibility of program elements.
- Non-Access Modifier: A modifier that provides additional functionality without affecting access levels.
- Serialization: The process of converting an object into a stream of bytes for storage or transmission.
- Thread: A lightweight process that allows multiple tasks to run simultaneously within a program.
Types of Modifiers in Java
- Access Modifiers
- Non-Access Modifiers
Access Modifiers in Java
Access modifiers control the visibility of classes, methods, variables and constructors. Java provides four access levels: Default (Package), Private, Protected and Public.
Default (Package) Access Modifier
When no access modifier is specified, Java uses the default access level. It is accessible within the same package only.
class Example {
int value = 10;
}Private Access Modifier in Java
The private modifier restricts access to members within the same class only. It cannot be accessed directly from other classes.
Example 01: Private Modifier
package Modifiers;
public class Private_modifier {
private int privateVariable = 20;
private void privateMethod() {
System.out.println("This is a private method.");
}
public static void main(String[] args) {
Private_modifier myObject = new Private_modifier();
System.out.println("Private variable value: " + myObject.privateVariable);
myObject.privateMethod();
}
}Public Access Modifier in Java
The public modifier makes members accessible from anywhere in the program. There are no access restrictions.
Example 02: Public Modifier
package Modifiers;
public class Public_modifier {
public int publicVariable = 10;
public void publicMethod() {
System.out.println("This is a public method.");
}
public static void main(String[] args) {
Public_modifier myObject = new Public_modifier();
System.out.println("Public variable value: " + myObject.publicVariable);
myObject.publicMethod();
}
}Protected Access Modifier in Java
The protected modifier allows access within the same package and by subclasses through inheritance.
Example 03: Protected Modifier
package Modifiers;
class Animal {
protected String name;
protected Animal(String name) {
this.name = name;
}
protected void speak() {
System.out.println(name + " makes a sound");
}
}
class Dog extends Animal {
public Dog(String name) {
super(name);
}
@Override
protected void speak() {
System.out.println(name + " barks");
}
}
public class Protected_modifier {
public static void main(String[] args) {
Dog myDog = new Dog("Buddy");
myDog.speak();
Animal myAnimal = new Animal("Generic Animal");
myAnimal.speak();
}
}Non-Access Modifiers in Java
Non-access modifiers do not control accessibility. They provide special functionalities to classes, methods and variables.
- final
- abstract
- static
- transient
- synchronized
- volatile
final Modifier in Java
The final modifier prevents modification. A final variable cannot be reassigned, a final method cannot be overridden and a final class cannot be inherited.
Example 04: final Modifier
package Modifiers;
public class Final_modifier {
final int x = 10;
final double PI = 3.14;
public static void main(String[] args) {
Final_modifier myObj = new Final_modifier();
// myObj.x = 50; // error: cannot assign a value to a final variable
// myObj.PI = 25; // error: cannot assign a value to a final variable
System.out.println(myObj.x);
}
}static Modifier in Java
The static modifier makes members belong to the class rather than individual objects. Static members can be accessed without creating an object and are shared among all instances.
Example 05: static Modifier
package Modifiers;
public class Static_Modifier {
static void myStaticMethod() {
System.out.println("Static methods can be called without creating objects");
}
public void myPublicMethod() {
System.out.println("Public methods must be called by creating objects");
}
public static void main(String[] args) {
myStaticMethod();
Static_Modifier myObj = new Static_Modifier();
myObj.myPublicMethod();
}
}abstract Modifier in Java
The abstract modifier is used with abstract classes and methods. Abstract classes cannot be instantiated. Abstract methods have no body and must be implemented by subclasses.
abstract void run();transient Modifier in Java
The transient modifier prevents variables from being serialized. Used when sensitive or temporary data should not be stored during object serialization.
synchronized Modifier in Java
The synchronized modifier allows only one thread to access a method at a time. It prevents race conditions and improves thread safety.
volatile Modifier in Java
The volatile modifier ensures that a variable's latest value is always read from main memory. It supports multithreading and prevents cached values from causing inconsistencies.
FAQ
1. What are modifiers in Java?
Modifiers are special keywords used to control accessibility and behavior of classes, methods, variables and constructors.
2. What is the difference between access and non-access modifiers?
Access modifiers control visibility, while non-access modifiers provide additional functionality such as inheritance control, threading and serialization.
3. What is the difference between public and private modifiers?
A public member can be accessed from anywhere, while a private member can only be accessed within the same class.
Where Java is used
Java in real-world development
Private Modifier
Restricts access to members within the same class only. Cannot be accessed from other classes.
Public Modifier
Makes members accessible from anywhere in the program with no access restrictions.
Protected Modifier
Allows access within the same package and through inheritance by subclasses.
final Modifier
Prevents variables from being reassigned, methods from being overridden and classes from being inherited.
static Modifier
Makes members belong to the class. Accessible without creating an object and shared across all instances.
abstract Modifier
Declares incomplete classes and methods. Subclasses must provide full implementation.