In Java, the static keyword is mostly used to control memory. It is also used to share a class’s identical variable or method. Static keywords can be used with variables, methods, blocks, and nested classes.
The static keyword refers to a class rather than a specific instance of that class. A constant variable or a method that is the same for every instance of a class is designated with the static keyword.
In Java, the static keyword is a non-access modifier that can be used for the following:
When a variable is declared as static, a single copy of the variable is made at the class level and shared among all objects. Static variables, on the other hand, are effectively global variables. The static variable is shared by all instances of the class.
Important considerations for static variables include:
The Java program below shows how static blocks and static variables are executed in the order in which they appear in a program.
class Student
{
int roll;
String name;
static String school = "Coderzpy High School";
public void show()
{
System.out.println(roll + "-" + name + "-" + school);
}
public static void main( String[] args )
{
Student s1 = new Student();
s1.roll = 11;
s1.name = "Abhijit";
s1.show();
Student s2 = new Student();
s2.roll = 28;
s2.name = "Ankit";
s2.show();
}
}
11-Abhijit-Coderzpy High School
28-Ankit-Coderzpy High School
The static method is defined as a method that is declared using the static keyword. The main() method is the most common example of a static method. Any static member of a class can be accessed before any objects of that class are generated, and without requiring a reference to any object.
Static methods are subject to the following constraints:
The java application to demonstrate static method constraints is shown below:
class Test
{
public static void color(String c)
{
System.out.println("color is "+c);
}
public static void main (String[] arg)
{
color("black"); //static method color() is called without any instance of class.
}
}
color is black
Static data members are initialized using the static block. The static block runs before the main() procedure.
You can declare a static block that gets executed exactly once when the class is first loaded if you need to do the computation to initialize your static variables.
Consider the Java program below, which demonstrates the use of static blocks.
class Test
{
static int a = 10;
static int b;
// static block
static {
System.out.println("Static block initialized.");
b = a * 4;
}
public static void main(String[] args)
{
System.out.println("from main");
System.out.println("Value of a : "+a);
System.out.println("Value of b : "+b);
}
}
Static block initialized.
from main
Value of a : 10
Value of b : 40
If a class is nested, it can only be made static. We can’t use the static modifier on top-level classes, but we can use it on nested classes. Nested static classes are an example of this type of class. A reference to the Outer class is not required for a nested static class. In this scenario, a static class can’t access the Outer class’s non-static members.
import java.io.*;
public class Demo {
private static String str = "Hello Coderzpy!";
// Static class
static class MyNestedClass {
// non-static method
public void disp(){
System.out.println(str);
}
}
public static void main(String args[])
{
Demo.MyNestedClass obj
= new Demo.MyNestedClass();
obj.disp();
}
}
Hello Coderzpy!
Note: also read about the Garbage Collection in Java
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…