Static keyword in Java
Table of Contents
2 sections
Static Keyword
A variable or function that is associated to a class i.e., is common to all objects
class Student {
String name;
int age;
static String school = "Doon Blossoms";
}
Student s1 = new Student();
System.out.println(Student.school);
Static Initialization Block
It can be used to initialize static fields of a class
public class Person {
static String name;
// Static Initialization Block
static {
name = "Arjit";
}
}
CSPrimer Newsletter
Enjoying this lesson?
Subscribe on Substack for monthly real-world architecture teardowns, curated reads, and new curriculum releases. 100% free, zero spam.