static and instance variable, how to access

Java Programming Language Class, Object and Methods in java (Article) Class, Object and Methods in java (Program)

891

Program:


public class StaticKeyword {
int p=23;
static int x = 20;

public static void main(String[] args) {

    System.out.println(x);  // no need to write  StaticKeyword.x
    StaticKeyword S1 = new StaticKeyword();
    StaticKeyword S2 = new StaticKeyword();
    System.out.println(x);  // no need to write  S1.x
    System.out.println(S2.p);   // you have to write  S2.p

 }
}

Output:

20
20
23
Press any key to continue . . .

This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.