Java-Access Static Variables without Object

Access Static Variables without Object

When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. Static variables can be accessed directly by the class name and doesn’t need any object.



Program Source

class Data{
    static int data1 = 40;
    static int data2 = 80;

public class Javaapp {
    public static void main(String[] args) {
        System.out.println("Data.data1 = "+Data.data1);
        System.out.println("Data.data2 = "+Data.data2);

Leave a Comment