zoukankan      html  css  js  c++  java
  • Static 1

    Sample first:

    ———————————-
    package ch.allenstudy.newway01;
    public class TestStatic {
    public static void main(String[] args) {
    A aa1 = new A();
    A aa2 = new A();

    aa1.i = 20; //Here, object “aa1″ sets its property i’s value to “20″.
    aa2.show(); //Here, object “aa2″ call the show() method which calls its own property value “i”, 10.
    }
    }
    class A {
    public int i = 10;
    public void show() {
    System.out.printf(“The value is: %d\n”, i);
    }
    }
    ———Result——–
    The value is: 10

    Now, let’s change property “i” to “static”. Now code is:
    ———————–
    package ch.allenstudy.newway01;

    public class TestStatic {

    public static void main(String[] args) {
    A aa1 = new A();
    A aa2 = new A();

    aa1.i = 20;
    aa2.show();
    }

    }

    class A {
    public static int i = 10;

    public void show() {
    System.out.printf(“The value is: %d\n”, i);
    }
    }
    What’s the result then??? Here u go!
    ———-Result———
    The value is: 20
    ————————-

    The result is 20 now! Why Why why?
    This is because: when we mark the property “i” as “static” (静态), in fact in memory, it is put from the “Stack” to another area called “data segment”. And in this area, the data are shared to all the objects. See below old picture.


    Once the “int i” is marked as static, in fact, it belongs to the CLASS itself, not a specific object.
    Let’s verify with below sample:
    —————————
    package ch.allenstudy.newway01;
    public class TestStatic {
    public static void main(String[] args) {
    System.out.printf(“%d\n”, A.i);
    }
    }
    class A {
    public int i = 10;
    }
    —————————
    Is this code block correct? Let’s try to run it. Get below exception
    ———-Exception———-
    Exception in thread “main” java.lang.Error: Unresolved compilation problem:
    Cannot make a static reference to the non-static field A.i
    at ch.allenstudy.newway01.TestStatic.main(TestStatic.java:6)
    —————————–
    It tells that we can’t call A.i directly, because “i” is not the property of the class A.
    Now we change the “i” to static:
    —————————-
    package ch.allenstudy.newway01;
    public class TestStatic {
    public static void main(String[] args) {
    System.out.printf(“%d\n”, A.i);
    }
    }
    class A {
    public static int i = 10;
    }
    —————————-
    Run the code, get result: 10

    So you see, after we add “static” before “int i”, the “i” in fact belongs to class, not a specific object. So you can call it directly.
    属于 class 的属性,即使我们没有创建对象,也可以直接调用。同理,属于 class 的方法,即使我们没有创建对象,我们也可以直接调用。 For example:
    —————————
    package ch.allenstudy.newway01;

    public class TestStatic {

    public static void main(String[] args) {
    System.out.printf(“%d\n”, A.i);
    A.show();
    }
    }

    class A {
    public static int i = 10;
    public static void show() {
    System.out.println(“Hello Allen!”);
    }

    }
    ———-Result—————–
    10
    Hello Allen!
    ——————————–
    See, we can call the static method directly either.

    那么,我们是否可以通过对象来访问类的静态属性和方法呢? Answer is Yes.
    ——————————-
    package ch.allenstudy.newway01;

    public class Recursion1
    {
    public static void main(String[] args)
    {
    AA aa = new AA();
    aa.show();
    }
    }
    class AA
    {
    public static int i = 10;
    protected static int j = 11;
    public static void show() //this is a static method (类的静态方法)
    {
    System.out.printf(“i= %d, j=%d\n”, i,j);
    }
    }
    ———Result———-
    i= 10, j=11

  • 相关阅读:
    java设计模式--桥接模式
    java设计模式--单例模式
    java设计模式--迭代器模式
    java设计模式--组合模式
    java设计模式--备忘录模式
    java设计模式--适配器模式
    洛谷P1464 Function
    洛谷P2434 [SDOI2005]区间
    p1416攻击火星
    p1359租用游艇
  • 原文地址:https://www.cnblogs.com/backpacker/p/2271548.html
Copyright © 2011-2022 走看看