the “static” keyword in Java
The following can be static in Java:
- Variable (also known as a class variable or class member)
- Method (also known as a class method)
- Block
- Nested class
when a variable is static in a java class
In a class, static variable is independent of the object of the class i.e., it is not unique for each object.
It can be accessed before any objects of its class are created (because it is independent of the object) and without reference to any object.
Accessing a static variable that belongs to a class:
class UniStudent{ int studentNumber; String name; static long totalNumOfStudents = 100; } public class TestStaticVariable1{ public static void main(String args[]){ // accessing a static variable is possible // without creating an object of that class System.out.println(UniStudent.totalNumOfStudents); } }
For example, in a university, the total number of students in the university is independent of the individual students
class UniStudent{ int studentNumber; String name; static long totalNumOfStudents = 100; // constructor UniStudent(int studentNumber, String name){ this.studentNumber = studentNumber; this.name = name; UniStudent.totalNumOfStudents += 1; } void display () { System.out.println(this.studentNumber+" "+this.name+" "+UniStudent.totalNumOfStudents); } } public class TestStaticVariable1{ public static void main(String args[]){ UniStudent s1 = new UniStudent(2018,"Sans"); UniStudent s2 = new UniStudent(2019,"Sunwoo"); s1.display(); s2.display(); } }
Output:
2018 Sans 102 2019 Sunwoo 102
when a method is static in java
A static method is a method that belongs to the class, not to any object
A static method can be invoked without the need for creating an instance (meaning object) of a class.
Example of calling a static method:
class UniStudent{ int studentNumber; String name; static long totalNumOfStudents = 100; //static method to change the value of static variable static void change(){ totalNumOfStudents = 100000; } } public class TestStaticVariable1{ public static void main(String args[]){ // calling the change method //A static method can be accessed directly // by the class name and doesn’t need any object UniStudent.change(); System.out.println(UniStudent.totalNumOfStudents); } }
Output: 100000
restriction of the static method:
The keywords 1.” this” and 2. “super” cannot be used in static context.
A static method can only access static variables, it CANNOT access non-static data without creating an instance.
Q. why is the main function in java static?
public static void main(String args[])
→ Because we don’t want to create an instance of the class to call/invoke main function
→ it must be accessible for an application to run, before any instantiation takes place.
static blocks in Java
Purpose of static block:
- initialize the static data member
How it works:
- executed before the main method, when the class is first loaded
Example:
class A { static String firstName = "Sans" static String fullName; // static block initializes fullName variable static { System.out.println("Static block initialized."); fullName = firstName + " Bhatia"; } public static void main(String args[]) { System.out.println(fullName); } }
Output:
Static block initialized. Sans Bhatia
Q. Can a program be executed without the main() method?
→ Not anymore after JDK 1.7
class A3{ static{ System.out.println("static block is invoked"); System.exit(0); } }
Output:
Error: Main method not found in class A3, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application