27. class - static data member
// class - static data member
/*
Static variables have a property of preserving
their value even after they are out of their
scope!Hence, static variables preserve their
previous value in their previous scope and
are not initialized again in the new scope.
*/
class number{
static int n;
}
class static_data_member
{
public static void main(String[] args) {
number.n = 12; // accese by class name
System.out.println("N = " + number.n);
number obj = new number();
obj.n = 32; // accese by object name
System.out.println("N = " + obj.n);
}
}
Comments
Post a Comment