next up previous contents
Next: Classes: Static Methods 1 Up: Java Notes Previous: Classes: Static Variables &

Classes: Static Variables 1

/**
 *  This code is from the book:
 *  Winder, R and Roberts, G (1998)
 *  Developing Java Software
 *  John Wiley & Sons.
 *  It is copyright (c) 1997 Russel Winder
 *  and Graham Roberts.
 */

import java.util.Vector;

class l1ClStat1{//Static1
  //Publically accessible constants
  public final static int UP = 1;
  public final static int DOWN = 2;

  //Publicly accessible class variable 
  //that can be changed by assignment.
  // BAD PROGRAMMING PRACTICE
  public static String s = "default";

  //Private class variables
  private static float f = 3.141f;
  private static Vector v = new Vector();

  //Static variables can be accessed from within a 
  //method; cannot actually tell whether they are 
  //class or instance variables from the usage.
  public void test(){
    int i = UP;
    s = "hello";
    v.addElement(s);
  }

  public static void main(String args[]){
    //Static variables can be accessed
    //from a static method
    s = "hello";
    v.addElement(s);
    l1ClStat1 s1 = new l1ClStat1();
    s1.test();
  }
}
/******** sample compilation & run ********
# javac l1ClStat1.java 
# java l1ClStat1
# 
******************************************/



Ananda Amatya
9/15/1999