Slide 1
Slide 2
Slide 3

class and static variables in java programming (V8)

No Comments

 
Class/Static Variables

  • Class variables, also known as static variables, are declared using the static keyword within a class but outside any method, constructor, or block.
  • There is only one copy of each class variable per class, regardless of how many objects are created from it.
  • Static variables are rarely used except for declaring constants. Constants are variables declared as public/private, final, and static. Constant variables never change from their initial value.
  • Static variables are stored in static memory. They are rarely used except when declared as final and used as public or private constants.
  • Static variables are created when the program starts and destroyed when the program stops.
  • Their usage is similar to instance variables; however, static variables are mostly declared as public because they need to be available to users of the class.
  • The default values are the same as for instance variables. For numbers, the default value is 0; for Booleans, it is false; and for object references, it is null. Values can be assigned during declaration or within a constructor. Additionally, values can also be assigned in special static initializer blocks.
  • Static variables can be accessed using the syntax ClassName.VariableName.
  • When declaring class variables as public static final, all variable names (constants) are written in uppercase letters. If static variables are not public and final, their naming syntax is the same as for instance and local variables.

        Static variables, also known as class variables, are variables declared in a class using the static keyword. Unlike instance variables, static variables belong to the class itself rather than to any specific object. This means they are shared among all objects of the class.

Syntax:

Source Code:

Copied!
class School {
    // Static variable shared by all instances of the class
    static String schoolName = "PIKT";
    
    // Instance variable specific to each student
    String studentName;

    // Constructor to initialize instance variable
    public School(String studentName) {
        this.studentName = studentName;
    }

    // Method to display student details
    public void displayStudentDetails() {
        System.out.println("Student Name: " + studentName);
        System.out.println("School Name: " + School.schoolName); 
        // Access static variable using ClassName.VariableName
    }
}

public class Static_variable {

	public static void main(String[] args) {
        // Accessing the static variable using ClassName
        System.out.println("Welcome to " + School.schoolName + "!");
        
        // Creating instances of School
        School student1 = new School("Alice");
        School student2 = new School("Bob");

        // Displaying details
        student1.displayStudentDetails();
        student2.displayStudentDetails();

        // Changing the static variable
        School.schoolName = "Polytechnic Institute of Kampong Thom Province";

        // Accessing updated static variable
        System.out.println("\nUpdated School Name:");
        student1.displayStudentDetails();
        student2.displayStudentDetails();
    }	
}

Result:

Watch the video:


Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

back to top