2016年11月4日星期五

Static Keyword Java

1. Why Static:


  • Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier.

2. Static fields


  • Fields that have the static modifier in their declaration are called static fields or class variables. 


  • Static fields are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory(). Any object can change it's value.

3. Static Methods

  • A common use for static methods is to access static fields. (不是静态的method也能access)
  • NO Override static methods:
    We can declare static methods with same signature in subclass, 
    but it is not considered overriding as there won’t be any run-time polymorphism. 
  • Instance methods can access instance variables and instance methods and class variables and class methods
  • Class methods can access class variables and class methods directly.
  • Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.
  • Static class cannot access outer class's instance variables and instance methods 
public class Bicycle {
    // add an instance variable for the object ID
    private int id;
    
    // add a class variable for the
    // number of Bicycle objects instantiated
    private static int numberOfBicycles = 0;
    
    public static int getNumberOfBicycles() {
        return numberOfBicycles;
    }

    public Bicycle(){
        // increment number of Bicycles
        // and assign ID number
        id = ++numberOfBicycles;
    }
}

class outer{  
    public static void test(){System.out.print("test");
    static class A{
static void print(){
System.out.print("A");
}
    }
    static class B extends A{
static void print(){
System.out.print("B");
                test();
}
, even if they are declared private.void nonStaticPrint(){
System.out.print("B");
                test();
}

    }
    A a = new A();
    a.print();====>"A"
    A b = new B();

    b.print();====>"A" 
    b.nonStaticPrint() ===> "B" /n "test"

Note: You can also refer to static fields or methods with an object reference 
but this is discouraged because it does not make it clear that they are class methods.

Constants

The static modifier, in combination with the final modifier, is also used to define constants. The final modifier indicates that the value of this field cannot change.

4.  Static Class

  • Only nested classes can be static.
  • What are the differences between static and non-static nested classes?  Non-static nested class is also called Inner Class.
    1) Nested static class doesn’t need reference of Outer class, Non-static nested class (Inner class) need.
    2) Inner class(or non-static nested class) can access both static and non-static members of Outer class, even if they are declared private.Also, because an inner class is associated with an instance, it cannot define any static members itself.  A static class cannot access non-static members of the Outer class. It can access only static members of Outer class.
    3) An instance of Inner class cannot be created without an instance of outer class and an Inner class can reference data and methods defined in Outer class in which it nests, so we don’t need to pass reference of an object to the constructor of the Inner class. For this reason Inner classes can make program simple and concise.(一个inner class 的实例需要outer class的实例来创建 outerInstacne.new A() Inner class可以access Outer class的members)
  • And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference.
int y = 0;
static class A{
int x;
void print(){
System.out.print(new test().y);
}
}

没有评论:

发表评论