Ticker

6/recent/ticker-posts

Java Static Variables — Java Static Method, Variable & Block

Java Static Variables — Java Static Method, Variable & Block

Static variables in Java are class-level variables that are shared among all instances of the class. They are associated with the class rather than with individual objects of the class. Static variables are declared using the static keyword and are initialized only once when the class is loaded into memory.

Declaration and Initialization of Static Variables

java
public class MyClass {
// Static variable declaration
public static int staticVariable;

// Static block for initialization
static {
staticVariable = 10;
}
}

Explanation:

  • In the example above, we have declared a static variable staticVariable of type int within the class MyClass.
  • The static block is used to initialize the static variable. It is executed only once when the class is loaded, and it occurs before the first access to any static member of the class.

Accessing Static Variables

Static variables can be accessed using the class name, followed by the dot operator, or directly within static methods of the class.

java
public class MyClass {
public static int staticVariable = 10;

public static void main(String[] args) {
// Accessing static variable using class name
System.out.println("Static variable: " + MyClass.staticVariable);

// Accessing static variable directly in a static method
printStaticVariable();
}

public static void printStaticVariable() {
System.out.println("Static variable from static method: " + staticVariable);
}
}

Output:


Static variable: 10
Static variable from static method: 10

Java Static Methods

Static methods in Java belong to the class and not to any particular instance of the class. They are declared using the static keyword and can only access other static members of the class. Static methods are commonly used for utility functions or operations that do not require any instance-specific data.

java
public class MathUtils {
// Static method to calculate the square of a number
public static int square(int num) {
return num * num;
}

// Static method to find the maximum of two numbers
public static int max(int a, int b) {
return (a > b) ? a : b;
}
}

Explanation:

  • In the example above, we have created a MathUtils class containing two static methods: square() and max().
  • These methods can be called directly using the class name, as they do not depend on any instance of the class.

Using Static Variables and Methods

java
public class Main {
public static void main(String[] args) {
// Using static variables
System.out.println("Static variable: " + MyClass.staticVariable);

// Using static method
int number = 5;
int squared = MathUtils.square(number);
System.out.println("Square of " + number + ": " + squared);

// Using another static method
int maxNum = MathUtils.max(10, 20);
System.out.println("Max number: " + maxNum);
}
}

Output:


Static variable: 10
Square of 5: 25
Max number: 20

Explanation:

  • The Main class demonstrates the usage of static variables and methods defined in MyClass and MathUtils, respectively.
  • We access the static variable staticVariable from MyClass and invoke static methods square() and max() from MathUtils.

Remember that static variables and methods should be used with caution, as they have a single shared value across all instances of the class and may lead to unintended side effects if not handled properly.

Post a Comment

0 Comments