Thursday, June 25, 2009

Learning Java : Static Variables and Static Methods

package Learn;
import java.lang.*;

public class implementingClass {
    static int count;      
           public implementingClass()
           {
               count = count+2;
           }

 static void  increment () {
    count = count+3;
  
}
    public static void main(String[] args) {
        System.out.println("Default value of count: " + count);
        implementingClass.increment();
        System.out.println("count: " + count);
    }
}

Output
Default value of count: 0
count: 3

Static variables are not tied to any object.They will be created at the time of class loading.Static methods can be called without object instantiation.



No comments: