Java Tutorial/Class Definition/static Member

Материал из Java эксперт
Перейти к: навигация, поиск

Define the static member

  1. You can use the keyword static in front of a field or method declaration.
  2. The static keyword may come before or after the access modifier.

These two are correct:



public static int a;
static public int b;



  1. From inside a static method, you cannot call instance methods or instance fields because they only exist after you create an object.
  2. You can access other static methods or fields from a static method.
  3. You can only declare a static variable in a class level.
  4. You cannot declare local static variables even if the method is static.


Demonstrates Static nested Classes

/*
 *     file: StaticNestedClassDemo.java
 *  package: oreilly.hcj.nested
 *
 * This software is granted under the terms of the Common Public License,
 * CPL, which may be found at the following URL:
 * http://www-124.ibm.ru/developerworks/oss/CPLv1.0.htm
 *
 * Copyright(c) 2003-2005 by the authors indicated in the @author tags.
 * All Rights are Reserved by the various authors.
 *
 ########## DO NOT EDIT ABOVE THIS LINE ########## */
/**
 * Demonstrates Static nested Classes.
 * 
 * @author 
   * @version $Revision: 1.5 $
   */
  private static class SomeOtherClass {
    /**
     * a demo method.
     */
    public void someMethod() {
      System.out.println("Protect me!");
    }
  }
}
/* ########## End of File ########## */





Demonstrate static variables, methods, and blocks.

class UseStatic {
  static int a = 3;
  static int b;
   
  static void meth(int x) {
    System.out.println("x = " + x);
    System.out.println("a = " + a);
    System.out.println("b = " + b);
  }
   
  static {
    System.out.println("Static block initialized.");
    b = a * 4;
  }
   
  public static void main(String args[]) {
    meth(42);
  }
}





Initializer for final static field

public class ClassInitializer7
{
   final static int classField;
   static
   {
      classField = 29;
   }
   public static void main (String [] args)
   {
      System.out.println (classField);
   }
}





Inside main( ), the static method callme( ) and the static variable b are accessed outside of their class.

class StaticDemo {
  static int a = 42;
  static int b = 99;
  static void callme() {
    System.out.println("a = " + a);
  }
}
   
class StaticByName {
  public static void main(String args[]) {
    StaticDemo.callme();
    System.out.println("b = " + StaticDemo.b);
  }
}





Reference inner static class

class MyClassesDemo {
  public static void main(String[] args) {
    MyClass tlc = new MyClass();
    MyClass.NestedMyClass ntlc;
    ntlc = new MyClass.NestedMyClass();
  }
}
class MyClass {
  private int i;
  private static String name = "MyClass";
  {
    System.out.println("Assigning 1 to i");
    i = 1;
  }
  static class NestedMyClass {
    int j;
    {
      System.out.println("Assigning 2 to j");
      j = 2;
      System.out.println(name);
    }
  }
}





reference static field after declaration

public class ClassInitializer3 {
  static int classField1 = 1;
  static int classField2 = 1 + classField1;
  public static void main(String[] args) {
    System.out.println(classField1);
    System.out.println(classField2);
  }
}





Static field, constructor and exception

public class Main {
  static Bar bar;
  static {
    try {
      bar = new Bar();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public static void main(String[] argv) {
    System.out.println(bar);
  }
}
class Bar {
  public Bar() throws Exception {
  }
  public String toString() {
    return "Bar";
  }
}





Static Members

  1. Static members are not tied to class instances.
  2. Static members can be called without having an instance.

The out field in java.lang.System is static.



public class MainClass {
  public static void main() {
    System.out.println("123");
  }
}



the method main(the entry point to a class) is static because it must be called before any object is created.


static section Initializer and logic

public class ClassInitializer6 {
  static int classField = 3;
  static {
    System.out.println(" : " + classField);
    classField = 1;
    for (int i = 2; i < 6; i++)
      classField *= i;
  }
  static {
    System.out.println(" = " + classField);
  }
  public static void main(String[] args) {
    System.out.println(classField);
  }
}





static section of Initializer

public class ClassInitializer5 {
  static String tz;
  static {
    java.util.Properties p = System.getProperties();
    p.list(System.out);
    tz = p.getProperty("user.timezone");
    if (tz.equals(""))
      tz = "Default";
  }
  public static void main(String[] args) {
    System.out.println("timezone = " + tz);
  }
}