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

  • 相关阅读:
    亚马逊的客户服务和承诺
    亚马逊云主管:我们有可能成为全世界最大的企业公司
    阿里市值超越亚马逊 马云开启下半场技术理想
    股价飙升500倍,市值超过4700亿美元,从网络书店起家的亚马逊凭什么一飞冲天?
    美国女子不断收到中国神秘快递,背后原因竟是刷单
    亚马逊称网络星期一成公司史上“最大的购物日
    万众创业葬送了多少人的前程
    日本企业遭遇严重用工短缺
    培生同意以3亿美元出售华尔街英语
    亚马逊500多名欧洲员工宣布在“黑色星期五”罢工
  • 原文地址:https://www.cnblogs.com/backpacker/p/2271548.html
Copyright © 2011-2022 走看看