Modifier
- Modifiers are keywords that precede the declaration of a class, interface, or its members, defining their scope, access level, and behavior within the application..
- Access Modifiers: public, protected, private, and default (no modifier), which control visibility and access of classes, methods, and variables.
- Non-Access Modifiers: static, final, abstract, synchronized, volatile, etc., which define additional properties like behavior, thread safety, or immutability.
public class JavaClass{
//here public is a modifier
}
Types of Modifiers
- There are two types of modifiers
- Access Modifiers
- Non-Access Modifiers
Access Modifiers:-
Access modifiers are those keywords that describe the rule of the access level of Classes, interfaces, and their members( i.e. - constructors, methods, variables, etc. )
Example:
package quipohouse;
class HelloWorld {
public void method1 () {….} //It can be access by other class
private void method2 () {….} //It can’t be access by other class
protected void method3 (){….} //It can be access within the pakcage and by its child
}
Types of Access Modifiers
There are Four types of access modifiers :
1. Default Access Modifier or package private:-
If we don't mark any access modifier( like public, private, protected) before declaring a class, interface, method, or other class members, the Java compiler by default gives the access within the package where it is declared.
Key Points:
- Class declared without a modifier is called as default access modifier.
- It can be accessed only within the package.
Example:
//File Name: Main.java
package quipohouse;
class Message
{
void msg()
{
System.out.println ("Hello to QUIPO HOUSE World");
}
}
public class Main
{
public static void main (String[]agrs) //main method always have pubic access-modifier.
{
Message obj = new Message ();
obj.msg ();
}
}
Output:
Hello to QUIPO HOUSE World
2. Public Access Modifier:-
- The class interface and its members declared public and can be accessible everywhere.
- It has no scope restriction.
Example:
//File 1 : HelloWorld.java
package quipohouse;
public class HelloWorld {
public void message() {
System.out.println(“Welcome to Quipo House”);
}
}
//File 2 : Main.java
package quipohouse;
public class Main
{
public static void main(String[] args)
{
HelloWorld obj = new HelloWorld ();
obj.message ();
}
}
Output:
Welcome to Quipo House
3. Private Access Modifier:-
- Class member marker as private cannot access outside its class.
- Except for the nested class, A class cannot be private.
Example:
//File : Main.java
package quipohouse;
class HelloWorld
{
private void message()
{
System.out.println("Quipo House");
}
}
public class Main
{
public static void main(String args[])
{
HelloWorld obj = new HelloWorld ();
//Trying to access private method message from another class
obj.message();
}
}
Output:
Main.java:17: error: message() has private access in HelloWorld
obj.message();
Example of the Access level of private modifier with class members (method, variable, etc.):
//File : Main.java
package quipohouse;
class Message
{
private String msg;
public String getMessage () // getter method
{
return this.msg;
}
public void setMessage (String msg) // setter method
{
this.msg = msg;
}
}
public class Main
{
public static void main (String[]main)
{
Message m = new Message ();
// accessing the private variable using the getter and setter method
m.setMessage ("Welcome To Quipo House");
System.out.println (m.getMessage ());
}
}
Output:
Welcome To Quipo House
4. Protected Access Modifier:-
- When the methods and variables are declared with a protected access modifier, we can access them within the same package as well as from its subclasses even outside the package where it is declared.
- A class cannot be protected.
Example:
//File : HelloWorld.java
public class HelloWorld
{
protected void message ()
{
System.out.println ("Welcome to Quipo House ");
}
}
//File : Main.java
public class Main extends HelloWorld
{
public static void main (String[]args)
{
HelloWorld obj = new HelloWorld ();
obj.message ();
}
}
Output:
Welcome to Quipo House
Top-level Classes cannot be private and protected.
Access Modifiers | Class | Package | outside the package |
Private | Yes | No | No |
Default | Yes | Yes | No |
Protected | Yes | Yes | Only in the Derived class |
Public | Yes | Yes | Yes |
Non-Access Modifier :-
The Non-access modifier affects the code behaviour other than its visibility or scope.
There are six non-access modifiers. We will separately study it further in this tutorial.
static
: Belongs to the class rather than instances (shared among all instances).final
: Prevents inheritance (for classes), method overriding, or variable reassignment.abstract
: Defines abstract methods (for classes) that must be implemented by subclasses.synchronized
: Ensures that a method or block of code is accessed by only one thread at a time.tansient
: Fields marked withtransient
are skipped during the serialization process, meaning their values will not be saved or transmitted when an object is serialized.synchronized
: Ensures that a method or block of code is accessed by only one thread at a time.