Java Program using a static variable


In this program, You will learn how to implement static variables in java.


static int x = 40;

Example: How to implement static variables in java.

class Main {

    int x = 10;
    static int y = 10;

    void change() {
        x++;
        y++;
    }

    void display() {
        System.out.println("x value is:" + x);
        System.out.println("y value is:" + y);
    }

    public static void main(String args[]) {
        Main t1 = new Main();
        t1.change();
        t1.display();

        Main t2 = new Main();
        t2.change();
        t2.display();
    }
}

Output:

x value is:11
y value is:11
x value is:11
y value is:12

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.