← Back to Core Java Course | Chapter 7: OOP Core | Lesson 6 of 11

Java Non-Access Modifiers

Non-access modifiers like static, final, and abstract control a class, method, or variable's behavior rather than its visibility, unlike access modifiers such as public or private.
Syntax
java
static dataType field;
final dataType field = value;
abstract void methodName();
synchronized void methodName() { }

What are Non-Access Modifiers?

Non-access modifiers control a class, method, or variable's behavior rather than its visibility -- keywords like static, final, and abstract change how a member works, unlike access modifiers such as public or private which control who can see it.

Example: What are Non-Access Modifiers?

java
class Circle {
	static final double PI = 3.14159; // static and final change behavior, not visibility
}
public class Main {
	public static void main(String[] args) {
		System.out.println(Circle.PI);
	}
}

The static Modifier

The static modifier attaches a field or method to the class itself rather than to individual objects, so a static field is shared by all instances and a static method can be called without creating an object first.

Example: The static Modifier

java
// Define the class Counter
class Counter {
	static int count = 0;
	static void increment() {
		count++;
	}
}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		Counter.increment();
		Counter.increment();
		// Print a line to the console
		System.out.println(Counter.count);
	}
}

The final Modifier

The final modifier prevents further change: a final variable can only be assigned once, a final method cannot be overridden by a subclass, and a final class cannot be extended by any other class.

Example: The final Modifier

java
final class Constants {
}
public class Main {
	public static void main(String[] args) {
		final int max = 100;
		// max = 200; // would not compile
		System.out.println(max);
	}
}

The abstract Modifier

The abstract modifier marks a class as unable to be instantiated directly and marks a method as having no body, requiring every concrete subclass to provide its own implementation of that method.

Example: The abstract Modifier

java
// Define the class Shape
abstract class Shape {
	abstract double area();
}
// Define the class Square
class Square extends Shape {
	// Declare side and set it to 4
	double side = 4;
	double area() {
		// Send side * side back to the caller
		return side * side;
	}
}
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println(new Square().area());
	}
}

Combining Non-Access Modifiers

Non-access modifiers can be combined on the same member when their meanings don't conflict, such as static final for a shared, unchangeable constant, or abstract and static appearing together in different contexts within an abstract class.

Example: Combining Non-Access Modifiers

java
class Config {
	static final int MAX_USERS = 100; // static + final together
}
public class Main {
	public static void main(String[] args) {
		System.out.println(Config.MAX_USERS);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using abstract together with final on a class or method, which is a compile error.
  2. Combining abstract with private or static on a method, which is not allowed.
  3. Calling a non-static member from a static method, which is a compile error.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.