Program for understanding difference between Static & non - Static variable

class Test {
public void show_non_static() {
int a = 6;
System.out.println(" non static " + a);
}
public static void Static_eg(int n1, int n2) {
System.out.println("The first parameter value is " + n1);
System.out.println("The second parameter value is " + n2);
}
public static void main(String args[]) {
Test n = new Test(); //creating object n for calling non static variable a
n.show_non_static();
Static_eg(5, 6);//calling of static does not need object creation
// n.Static_eg(5, 6); //static variable may be accessed by non static method i.e obj creation
}
}


output

non static 6
The first parameter value is 5
The second parameter value is 6


No comments:

Post a Comment