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

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.

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();
}
}
> Toyota BXV213
Toyota CRD442
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);
}
}
> 125

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;

  • 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
  • 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.
public class OuterClass {
class InnerClass{

}
}
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);
}
}
> This is the Outer class
This is the Inner 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);
}
}
/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 is the Inner class

Data Scientist/Software Developer.. Aspiring robotics engineer. Tech and Artificial Intelligence enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store