Static in Java
In Java, static is a keyword or modifier that indicates a member belongs to the class rather than to instances of the class. This means that an instance of the class is not required to access static members. In Java, static members can include variables (fields), methods, blocks, and nested classes. You can access static members from another class using the class name. If you’re accessing them within the same class, they can be accessed directly without the class name.
Static members are commonly used for methods and fields and may also include other static components as needed.
Example 1: Accessing Static Members within the Same Class
In this example, we define a static method displayData and a static field value in the same class and access them without using the class name.
public class Person {
public static int value = 24;
public static void displayData() {
String name = “Hamza”;
int age = 46;
System.out.println(“Name: ” + name);
System.out.println(“Age: ” + age);
}
public static void main(String[] args) {
System.out.println(value); // Access static field
displayData(); // Call static method
}
}
Output:
24
Name: Hamza
Age: 46
Explanation :
public class Person
Defines a class named Person.
public static int value = 24;
Declares and initializes a public static integer field value with the value 24.
public static void displayData()
Defines a static method displayData within the Person class.
System.out.println(“Name: ” + name);
Outputs the name variable to the console.
System.out.println(“Age: ” + age);
Outputs the age variable to the console.
public static void main(String[] args)
Entry point of the application.
System.out.println(value);
Prints the value field of the Person class directly.
displayData();
Calls the displayData method to display the name and age.
Example 2: Accessing Static Members from Another Class
In this example, we access the static field value and the static method displayData from another class using the class name.
public class Person {
public static int value = 24;
public static void displayData() {
String name = “Hamza”;
int age = 46;
System.out.println(“Name: ” + name);
System.out.println(“Age: ” + age);
}
}
public class Program {
public static void main(String[] args) {
System.out.println(Person.value); // Access static field using class name
Person.displayData(); // Call static method using class name
}
}
Output:
24
Name: Hamza
Age: 46
Explanation:
public class Person
Defines a class named Person.
public static int value = 24;
Declares and initializes a public static integer field value with the value 24.
public static void displayData()
Defines a static method displayData within the Person class.
public class Program
Defines a second class Program.
public static void main(String[] args)
Entry point of the Program application.
System.out.println(Person.value);
Accesses and prints the value field of the Person class using the class name Person.
Person.displayData();
Calls the displayData method of the Person class using the class name to display the name and age.
Example 3: Accessing Non-Static Members with an Instance
In this example, we make value and displayData non-static and access them by creating an instance of the Person class.
public class Person {
public int value = 24;
public void displayData() {
String name = “Hamza”;
int age = 46;
System.out.println(“Name: ” + name);
System.out.println(“Age: ” + age);
}
}
public class Program {
public static void main(String[] args) {
Person personInstance = new Person(); // Create an instance of Person
System.out.println(personInstance.value); // Access non-static field
personInstance.displayData(); // Call non-static method
}
}
Output:
24
Name: Hamza
Age: 46
Explanation:
public class Person
Defines a class named Person.
public int value = 24;
Declares and initializes a public integer field value with the value 24.
public void displayData()
Defines a non-static method displayData within the Person class.
public class Program
Defines a second class Program.
public static void main(String[] args)
Entry point of the Program application.
Person personInstance = new Person();
Creates an instance personInstance of the Person class.
System.out.println(personInstance.value);
Accesses and prints the value field through the instance.
personInstance.displayData();
Calls the displayData method using the instance personInstance to display the name and age.
Differences
Task:
1. Static Field:
● Create a class named MathHelper with a static field PI of type double, initialized to 3.14.
● Add a static method CalculateCircleArea(double radius) that calculates and returns the area of a circle using the
formula PI * radius * radius.
● In the main method, use the static field and method to calculate and print the area of a circle with radius 5.
2. Static Property:
● Create a class named Counter with a static property Count of type int.
● Implement a static method IncrementCount() that increments the Count property by 1.
● Create multiple objects of the Counter class and call the IncrementCount() method several times to show how the
static property Count increments.
3. Static Constructor:
● Create a class named Logger with a static field logFilePath initialized to a default log file path.
● Implement a static constructor that initializes logFilePath based on the current date, e.g., “log-2024-07-06.txt”.
● Create instances of the Logger class to demonstrate how the static constructor initializes logFilePath.
4. Static Method:
● Create a class named Utility with a static method IsPrime(int number) that checks if the specified number is a
prime number.
● In the main method, use this static method to check and print whether numbers like 7, 12, and 23 are prime.
5. Static Class:
● Create a static class named Constants with constant fields for mathematical constants, such as PI, Euler’s
number (e), and the Golden ratio.
● Use these constant fields in a simple calculation (e.g., area of a circle using PI) in the main method and print the
results.
Course Video
YouTube Reference :
It’s a free online course on the Java static
keyword, covering static variables, methods, and classes.
You’ll learn how to use the static
keyword for defining static variables, methods, and classes with practical examples.
A static variable is shared across all instances of a class, meaning it belongs to the class rather than any specific object.
Static methods belong to the class and can be called without creating an object, while instance methods require an object to be invoked.
A static class is a nested class that doesn’t require an instance of the outer class to be accessed.
Yes, the course provides examples of static variables, methods, and nested classes.
The static
keyword helps in memory optimization by sharing common data and enabling utility methods.
No, the this
keyword cannot be used in static methods or blocks because they don’t operate on instance data.
Yes, the course explains static blocks and how they are used for initializing static variables.
Yes, it’s designed for beginners with simple explanations and practical examples.