3. Program with class variable that is available for all instances of a class. Use static variable declaration. Observe the changes that occur in object’s member variable values.
import java.io.*;
class Prog3
{
static int num1; //Class Variable
int num2;
public static void main(Stringargs[])
{
Prog3.num1=30;
Prog3 obj1=new Prog3();
Prog3 obj2=new Prog3();
obj1.num2=25;
obj2.num2=50;
System.out.println("Assigning Prog3.num1=30, obj1.num2=25 and
obj2.num2=50");
System.out.println("Prog3.num1="+Prog3.num1);
System.out.println("obj1.num2="+obj1.num2);
System.out.println("obj2.num2="+obj2.num2);
obj1.num1=45;
obj1.num2=67;
System.out.println("\nAfter changing class variable with
obj1.num1=45 and obj1.num2=67");
System.out.println("Prog3.num1="+Prog3.num1);
System.out.println("obj1.num1="+obj1.num1);
System.out.println("obj2.num1="+obj2.num1);
System.out.println("obj1.num2="+obj1.num2);
System.out.println("obj2.num2="+obj2.num2);
}
}
No comments :
Post a Comment