Understanding OOP in Java (‘static’ keyword, Nested classes and Access Modifiers)

The Javatar
4 min readDec 20, 2020
Photo by Scott Graham on Unsplash

Static keyword

The idea behind the static keyword is to manage memory; it is used to create fields and methods that belong to the class rather than the object(instance of the class). When objects of a class are created, copies of the instance variables are created per object and are stored in different memory locations. The static keyword is used in a situation where you want to create variables common to all objects; it can be applied to fields and methods and even blocks and nested classes.

  • Java Static Variable

A variable is declared static if it is a common property to every object of the class. A static variable is also known as a class variable as it belongs to the class. It is invoked by using the class name(good practice) rather than using the instance name.

public class Car{
int production_no;
String model;
static String brand="Toyota";
Car(int num, String model_code){
production_no=num;
model=model_code;
}
void get_car_name(){
System.out.println(brand+" "+model+production_no);
}

public static void main(String [] args){
Car a = new Car(213,"BXV");
Car b = new Car(442,"CRD");
a.get_car_name();
b.get_car_name();
}
}

Output:

> Toyota BXV213
Toyota CRD442

In this example, declaring the brand variable static means it is only assigned memory once, rather than every time a new object is created.

  • Java static methods

Java also supports static methods aka class methods. They are also invoked using class names i.e className.method(). It can also be invoked without a need for creating an object of the class.

A few things to note; a static method can only access static methods and variables directly. Static methods cannot use the this or super keyword as there is no instance to be referred. Example of a static method;

class Calculate{
static int cube(int x){
return x*x*x;
}
public static void main(String args[]){
int result=Calculate.cube(5);
System.out.println(result);
}
}

Output;

> 125

For references and more reading, visit;

Access Modifiers

Access modifiers specifies the scope of which a class, constructor, interface, method or field can be accessed. There are 4 access modifiers in Java;

  • Default: This access modifier doesn’t have a keyword. Default classes, interfaces, methods or fields are only accessible within the package. They cannot be accessed anywhere outside the package.
  • Private: Fields, constructors and method declared private can only be accessed within the class they’re in. When constructors are declared private, it means that an instance of that class cannot be created outside the class. Note that classes and interfaces cannot be declared private except inner(nested) classes.
private void method(){
System.out.println("A private method");
}
private String field_name; //private variable
  • Public: When an entity is declared public, it means it can be accessed anywhere. It has the widest scope of accessibility.
  • Protected: A protected field, constructor or method can only be accessed within the package or in a child class of that class by Inheritance which will be discussed later in this series. A class and interface cannot also be declared protected.

Nested Classes

Nesting classes is supported in Java. It allows to logically group related classes together thereby making your code more readable, maintainable and allows to achieve encapsulation. There are two types of nested classes: static and non-static nested classes.

The syntax to nest classes together is

public class OuterClass {
class InnerClass{

}
}

You can access the inner class and it’s members by creating the object of the outer class and then the object of the inner class.

public class OuterClass{
String a = "This is the Outer class";
class InnerClass{
String b = "This is the Inner class";
}
public static void main(String args[]){
OuterClass outer = new OuterClass();
OuterClass.InnerClass inner = outer.new InnerClass();
System.out.println(outer.a);
System.out.println(inner.b);
}
}

Output

> This is the Outer class
This is the Inner class

An Inner class can be declared as private or protected as earlier stated. This means that members of the inner class cannot be accessed outside the outer class.

public class OuterClass{
String a = "This is the Outer class";
private class InnerClass{
String b = "This is the Inner class";
}
}
class newClass{
public static void main(String args[]){
OuterClass outer = new OuterClass();
OuterClass.InnerClass inner = outer.new InnerClass();
System.out.println(outer.a);
System.out.println(inner.b);
}
}

The above program will give an error

/OuterClass.java:10: error: OuterClass.InnerClass has private access in OuterClass
OuterClass.InnerClass inner = outer.new InnerClass();
^
/OuterClass.java:10: error: OuterClass.InnerClass has private access in OuterClass
OuterClass.InnerClass inner = outer.new InnerClass();
^
2 errors

Static Inner Class

An inner class can be declared static, which means the inner class can be accessed without creating an instance of the outer class

class OuterClass {   static class InnerClass {
String y = "This is the Inner class";
}
}
public class newClass{
public static void main(String[] args) {
OuterClass.InnerClass myInner = new OuterClass.InnerClass();
System.out.println(myInner.y);
}
}

This outputs:

> This is the Inner class

Note that a static inner class does not also have access to non-static members of the outer class

Read more about Nested classes at

I hope this article was able to simplify these topics to you. check out my previous article in this series

--

--

The Javatar

An adventure to becoming a fully realized Javatar 😂😎