- Java Tutorial
- Java Introduction
- Java Features
- Java Simple Program
- JVM, JDK and JRE
- Java Syntax
- Java Comments
- Java Keywords
- Java Variables
- Java Literals
- Java Separators
- Java Datatypes
- Java Operators
- Java Statements
- Java Strings
- Java Arrays
- Control Statement
- Java If
- Java If-else
- Java If-else-if
- Java Nested If
- Java Switch
- Iteration Statement
- Java For Loop
- Java For Each Loop
- Java While Loop
- Java Do While Loop
- Java Nested Loop
- Java Break/Continue
- Java Methods
- Java Methods
- Java Method Parameters
- Java Method Overloading
- Java Recursion
- Java OOPS
- Java OOPs
- Java Classes/Objects
- Java Inheritance
- Java Polymorphism
- Java Encapsulation
- Java Abstraction
- Java Modifiers
- Java Constructors
- Java Interface
- Java static keyword
- Java this keyword
- Java File Handling
- Java File
- Java Create File
- Java Read/Write File
- Java Delete File
- Java Program To
- Add Two Numbers
- Even or Odd Numbers
- Reverse a String
- Swap Two Numbers
- Prime Number
- Fibonacci Sequence
- Palindrome Strings
- Java Reference
- Java String Methods
- Java Math Methods
Java Modifiers
Modifiers in Java are keywords that are used to define the scope, behavior, and accessibility of classes, variables, methods, and other elements in a Java program. Here's an overview of commonly used modifiers:
Access Modifiers:
- Control the accessibility of classes, variables, and methods.
- Four types: public, protected, default (no modifier), and private.
- Example:
public class MyClass {
private int x;
protected void display() {
System.out.println("Hello");
}
}
Non-Access Modifiers:
- Define additional properties and behavior of classes, variables, and methods.
- Includes static, final, abstract, synchronized, transient, and volatile.
- Example:
public class MyClass {
static int count;
final double PI = 3.14;
abstract void draw();
}
Static Modifier:
- Used to declare variables and methods as class-level instead of instance-level.
- Static variables are shared among all instances of a class.
- Example:
class Counter {
static int count = 0;
Counter() {
count++;
}
}
Final Modifier:
- Indicates that a variable, method, or class cannot be modified or overridden.
- Final variables cannot be reassigned once initialized.
- Example:
final int MAX_VALUE = 100;
Abstract Modifier:
- Used to declare abstract classes and methods.
- Abstract classes cannot be instantiated, and abstract methods must be implemented by subclasses.
- Example:
abstract class Shape {
abstract void draw();
}
Summary
Modifiers provide control over the accessibility, behavior, and properties of elements in a Java program, allowing for better organization, encapsulation, and reusability of code. Understanding how to use modifiers effectively is essential for writing efficient and maintainable Java applications.