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

  • 相关阅读:
    Neptune w800开发版ubuntu linux环境编译通过——如何搭建开发环境
    neptune HarmonyOS开发板视频教程-环境搭建-编译-烧录-外设控制
    Neptune开发板与hiSpark开发板底板的管脚不兼容
    《设计心理学》要点(上)
    NLP(三十二):大规模向量相似度检索方案
    VIM编辑器设置
    (十一)pytorch加速训练的17种方法
    (十)pytorch多线程训练,DataLoader的num_works参数设置
    NLP(三十一):用transformers库的BertForSequenceClassification实现文本分类
    NLP(三十):BertForSequenceClassification:Kaggle的bert文本分类,基于transformers的BERT分类
  • 原文地址:https://www.cnblogs.com/backpacker/p/2271548.html
Copyright © 2011-2022 走看看