Tutorial 14: Static Variables
Sample Code:
MainStaticDemo.java
package StaticDemo;
/**
*
* @author Ankit
*/
public class MainStaticDemo {
public static void main(String[] args) {
StaticVariables.iNo1 = 0;
StaticVariables o1 = new StaticVariables();
StaticVariables o2 = new StaticVariables();
//o1 has its own iNo2;
//o2 has its own iNo2;
o1.iNo2 = 10;
o2.iNo2 = 20;
//o1 and o2 shares iNo1; /// static
System.out.println("o1's no2 is:"+o1.iNo2);
System.out.println("o2's no2 is:"+o2.iNo2);
System.out.println("iNo1 is:"+StaticVariables.iNo1);
}
}
/**
*
* @author Ankit
*/
public class MainStaticDemo {
public static void main(String[] args) {
StaticVariables.iNo1 = 0;
StaticVariables o1 = new StaticVariables();
StaticVariables o2 = new StaticVariables();
//o1 has its own iNo2;
//o2 has its own iNo2;
o1.iNo2 = 10;
o2.iNo2 = 20;
//o1 and o2 shares iNo1; /// static
System.out.println("o1's no2 is:"+o1.iNo2);
System.out.println("o2's no2 is:"+o2.iNo2);
System.out.println("iNo1 is:"+StaticVariables.iNo1);
}
}
StaticVariables.java
package StaticDemo;
/**
*
* @author Ankit
*/
public class StaticVariables {
static int iNo1;
int iNo2;
public StaticVariables() {
iNo1++;
}
}
/**
*
* @author Ankit
*/
public class StaticVariables {
static int iNo1;
int iNo2;
public StaticVariables() {
iNo1++;
}
}
No comments:
Post a Comment