Access Modifiers, Static & Instance Rules
These rules become much easier if you remember three things:
-
Where is the class declared?
-
Is it static or instance?
-
Who is allowed to access it?
1. Access Modifiers
| Modifier | Accessible from |
|---|---|
public |
Anywhere |
protected |
Same package + subclasses |
| default | Same package |
private |
Same class |
Example:
class Person {
public String name;
protected int age;
String city; // default
private String password;
}
2. Top-Level Classes
A top-level class is a class declared directly in a file.
public class Person {
}
A top-level class can only be:
public class Person {}
or:
class Person {}
It cannot be private, protected, or static.
It can contain both static and instance members:
class Person {
static String country = "India";
String name;
static void hello() {}
void introduce() {}
}
Remember
Top-level class →
publicor default → neverstatic.
3. Static Nested Class
A class inside another class using static.
class Outer {
static int x = 10;
int y = 20;
static class Inner {
void show() {
System.out.println(x); // OK
// System.out.println(y); // ERROR
}
}
}
A static nested class:
-
Can have static and instance members.
-
Can access outer static members directly.
-
Cannot access outer instance members directly.
Remember
Static nested class → no outer object needed.
4. Member Inner Class
A non-static class declared inside another class.
class Outer {
private int value = 10;
class Inner {
void show() {
System.out.println(value); // OK
}
}
}
The inner class can access all members of the outer class, including private members.
To create it:
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
Remember
Inner class → connected to an outer object.
5. Local Inner Class
A class declared inside a method, constructor, or block.
class Outer {
void display() {
int number = 10;
class Local {
void show() {
System.out.println(number);
}
}
Local obj = new Local();
obj.show();
}
}
The local variable must be final or effectively final:
int number = 10; // effectively final
Remember
Local inner class → class inside a method, usually for local use.
6. Anonymous Inner Class
A class with no name, created and used immediately.
Commonly used with interfaces or abstract classes.
Runnable task = new Runnable() {
@Override
public void run() {
System.out.println("Running");
}
};
task.run();
Remember
Anonymous class → one-time implementation.
7. Static vs Instance
Static
Belongs to the class.
class Counter {
static int count = 0;
}
All objects share count.
Access without creating an object:
Counter.count;
A static method can directly access only static members:
class Test {
static int x = 10;
int y = 20;
static void show() {
System.out.println(x); // OK
// System.out.println(y); // ERROR
}
}
Instance
Belongs to an object.
class Person {
String name;
void show() {
System.out.println(name);
}
}
You need an object:
Person p = new Person();
p.show();
An instance method can access both static and instance members.
8. Interfaces
Interfaces can contain different types of methods.
interface Animal {
void sound(); // abstract
default void eat() {
System.out.println("Eating");
}
static void info() {
System.out.println("Animal interface");
}
private void helper() {
System.out.println("Helper");
}
}
Remember
| Member | Meaning |
|---|---|
| Abstract method | Must be implemented |
default method |
Has implementation, can be overridden |
static method |
Belongs to interface, cannot be overridden |
private method |
Only used inside interface |
| Variable | Always public static final |
Example:
interface Math {
int VALUE = 10;
}
VALUE is automatically:
public static final int VALUE = 10;
9. Implementing Interface Methods
Interface methods are normally public.
interface Animal {
void sound();
}
class Dog implements Animal {
@Override
public void sound() {
System.out.println("Woof");
}
}
You cannot reduce visibility:
class Dog implements Animal {
protected void sound() { // ERROR
}
}
Because the interface method is public.
10. Abstract Classes
An abstract class can contain almost everything a normal class can contain.
abstract class Animal {
private String name;
Animal(String name) {
this.name = name;
}
abstract void sound();
void eat() {
System.out.println("Eating");
}
static void info() {
System.out.println("Animal");
}
}
It can have:
-
Abstract methods
-
Normal methods
-
Static methods
-
Instance methods
-
Static variables
-
Instance variables
-
Constructors
-
Any normal access modifier
But you cannot create its object directly:
Animal a = new Animal("Dog"); // ERROR
Quick Exam Summary
| Type | static? |
Static members? | Main idea |
|---|---|---|---|
| Top-level class | ❌ | ✅ | Normal outer class |
| Static nested class | ✅ | ✅ | Independent of outer object |
| Member inner class | ❌ | ✅ in modern Java | Connected to outer object |
| Local class | ❌ | Modern Java has relaxed rules | Inside method/block |
| Anonymous class | ❌ | Limited/usually unnecessary | One-time implementation |
| Interface | N/A | ✅ | Contract |
| Abstract class | Depends | ✅ | Shared code + abstraction |
Most important rules
Top-level class → cannot be
static.
Static method → directly accesses static members only.
Instance method → can access static + instance members.
Interface variables → always
public static final.
Interface methods → public by default;
privatemethods are allowed from Java 9.
Abstract class → can have both abstract and concrete methods.