GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Java static keyword - static variables, methods, blocks, imports Java static keywordlast modified May 1, 2025
In this article we cover the Definition
The
Additionally, the
Usage of the static Keyword
The
Java static variable
Static variables are also known as class variables. All instances of a class
share the same copy of a static variable. They are initialized only once, at
the start of the execution. A class variable can be accessed directly by the
class name, without the need to create a instance. One common use of
Static variable exampleMain.java
import java.util.ArrayList;
import java.util.List;
class Being {
public static int count;
}
class Cat extends Being {
public Cat() {
count++;
}
}
class Dog extends Being {
public Dog() {
count++;
}
}
class Donkey extends Being {
public Donkey() {
count++;
}
}
void main(String[] args) {
List<Being> beings = new ArrayList<>();
beings.add(new Cat());
beings.add(new Cat());
beings.add(new Cat());
beings.add(new Dog());
beings.add(new Donkey());
int nOfBeings = Being.count;
System.out.format("There are %d beings %n", nOfBeings);
}
In the code example, we keep track of beings created with a static variable. class Being {
public static int count;
}
A static variable is defined. The variable belongs to the class Cat extends Being {
public Cat() {
count++;
}
}
The class Dog extends Being {
public Dog() {
count++;
}
}
The int nOfBeings = Being.count; We get the number of all beings created. We refer to the class variable by its class name followed by the dot operator and the variable name. Java static variable properties
Java static method
Static methods are called without an instance of the object. To call a static
method, we use the name of the class, the dot operator, and the name of the
method. Static methods can only work with static variables. Static methods are
often used to represent data or calculations that do not change in response to
object state. For instance,
We use the Static method restrictions
Static method can only call other static methods. They can only access static
data and cannot refer to Static method examplecom/zetcode/Main.java
package com.zetcode;
class Basic {
static int id = 2321;
public static void showInfo() {
System.out.println("This is Basic class");
System.out.format("The Id is: %d%n", id);
}
}
public class Main {
public static void main(String[] args) {
Basic.showInfo();
}
}
In our code example, we define a static static int id = 2321; A static method can only work with static variables. Static variables are not available to instance methods. public static void showInfo() {
System.out.println("This is Basic class");
System.out.format("The Id is: %d%n", id);
}
This is our static Basic.showInfo(); To invoke a static method, we do not need an object instance. We call the method by using the name of the class and the dot operator. The static main methodIn Java console and GUI applications, the entry point has the following signature: public static void main(String[] args)
By declaring the
In addition, if the Java static blockA code block with the static modifier is called a class initializer. A code block without the static modifier is an instance initializer. Class initializers are executed in the order they are defined, top down, when the class is loaded. A static block executes once in the life cycle of any program, and there is no other way to invoke it. Static block exampleMain.java
package com.zetcode;
public class Main {
private static final int i;
static {
System.out.println("Class initializer called");
i = 6;
}
public static void main(String[] args) {
System.out.println(i);
}
}
This is an example of a static initializer. static {
System.out.println("Class initializer called");
i = 6;
}
In the static initializer, we print a message to the console and initialize a static variable. Static nested classesA static nested class is a nested class that can be created without the instance of the enclosing class. It has access to the static variables and methods of the enclosing class. Static nested classes can logically group classes that are only used in one place. They increase encapsulation and provide more readable and maintainable code. Static nested classes restrictionsStatic nested classes cannot invoke non-static methods or access non-static fields of an instance of the enclosing class. Static nested class exampleMain.java
package com.zetcode;
public class Main {
private static int x = 5;
static class Nested {
@Override
public String toString() {
return "This is a static nested class; x:" + x;
}
}
public static void main(String[] args) {
Main.Nested sn = new Main.Nested();
System.out.println(sn);
}
}
The example presents a static nested class. private static int x = 5;
This is a private static variable of the static class Nested {
@Override
public String toString() {
return "This is a static nested class; x:" + x;
}
}
A static nested class is defined. It has one method which prints a message
and refers to the static Main.Nested sn = new Main.Nested(); The dot operator is used to refer to the nested class. Java static import
Static imports allow members (fields and methods) defined in a class as
Static import disadvantagesThe overuse the static import feature can make our program unreadable and unmaintainable, polluting its namespace with all the static members we import. Static import exampleMain.java
package com.zetcode;
import static java.lang.Math.PI;
public class Main {
public static void main(String[] args) {
System.out.println(PI);
}
}
In the example, we use the Java constants
The public static final double PI = 3.14159265358979323846;
For example, in Singleton patternSingleton design pattern ensures that one and only one object of a particular class is ever constructed during the lifetime of the application. Singleton.java
public class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton() {}
public static Singleton getInstance() {
return INSTANCE;
}
}
In this simple code excerpt, we have an internal static reference to the single allowed object instance. We access the object via a static method. Source
In this article we have presented the Java AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Java tutorials. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |