Code for this tutorial:
MainStaticDemo.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package StaticDemo;
/**
*
* @author Ankit
*/
public class MainStaticDemo {
static{
System.out.println("I am before main....");
}
//javac MainStatic.java
//java MainStatic
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.callMeStatic();
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package StaticDemo;
/**
*
* @author Ankit
*/
public class MainStaticDemo {
static{
System.out.println("I am before main....");
}
//javac MainStatic.java
//java MainStatic
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.callMeStatic();
}
}
StaticVariables,java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package StaticDemo;
/**
*
* @author Ankit
*/
public class StaticVariables {
static int iNo1;
int iNo2;
public StaticVariables() {
iNo1++;
}
public static void callMeStatic(){
System.out.println("I am from static method of class StaticVari.");
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package StaticDemo;
/**
*
* @author Ankit
*/
public class StaticVariables {
static int iNo1;
int iNo2;
public StaticVariables() {
iNo1++;
}
public static void callMeStatic(){
System.out.println("I am from static method of class StaticVari.");
}
}
No comments:
Post a Comment