Java serialization 43 - other considerations for accessing static methods, static keywords

Posted by hrichman on Sun, 20 Oct 2019 20:16:43 +0200

I. other precautions

1. For methods with static, we said that we need to use the method of "class name", but in fact, we can use the method of "reference" to access this method.

 

package com.bjpowernode.java_learning;

​

public class D43_ {

  public static void main(String[] args) {

    Test3.test3();

    Test3 t1 = new Test3();

    t1.test3();

  }

}

class Test3{

  String name;

  public Test3() {}

  public static void test3(){

    System.out.println("I can execute");

  }

}

 

2. For the above features, why? I want to use the following further example. Maybe we can know one or two. We talked about null pointer exception before.

This null pointer exception means that the data related to the null reference accessing instance is the data related to the object. When accessing the data, there must be the participation of the object. When the null reference, the object does not exist, and the null pointer exception will appear when accessing the data.

We're following the above procedure and adding an executive procedure.

 

    t1 = null;

    t1.test3();

 

We can see that even null pointer can execute without null pointer exception, which shows that t1 is not used at all. During execution, the system will still rewrite Test3.test3() to execute.

But this way is not recommended. Although it can be implemented, it is not our logic.

2. Static variables

1. First, look at the code of object creation.

 

package com.bjpowernode.java_learning;

​

public class D43_2_static_keyword {

  public static void main(String[] args) {

    Chinese c1 = new Chinese("zhangsan",32,"China");

    Chinese c1 = new Chinese("lisi",34,"China");   

  }

}

class Chinese{

  String name;

  int id;

  String country;

  public Chinese(String name,int id,String country) {

    this.name = name;

    this.id = id;

    this.country = country;

  }

}

 

 

Explanation: we have created two Chinese objects whose nationality is China, but we have to write China once every time we create them, which results in code redundancy. When we remove this instance variable, the scene is unreasonable, and we need such attribute values. Therefore, we have common attributes for this object, leading to the concept of static variables.

2. Static variables, that is, in the code of the class, for instance variables, we add a static keyword to represent that this attribute is a common attribute of all created objects.

(1) static variables are initialized when the class is loaded. There is no need to create objects, and memory is opened up.

(2) static variables are stored in method area memory.

Example:

 

package com.bjpowernode.java_learning;

​

public class D43_2_static_keyword {

  public static void main(String[] args) {

    Chinese c1 = new Chinese("zhangsan",32);

    System.out.println(c1.country);

    System.out.println(Chinese.country);

    c1 = null;

    System.out.println(c1.country);

  }

}

class Chinese{

  String name;

  int id;

  static String country = "China";

  public Chinese(String name,int id) {

    this.name = name;

    this.id = id;

  }

  pubic Chinese() {}

}

For example, we can see that when we access this instance variable, we can use "class", "reference" and "reference". All of them are successful. Moreover, the principle is similar to the null pointer described above. Here are a few more examples.

 

package com.bjpowernode.java_learning;

​

public class D43_2_static_keyword {

  public static void main(String[] args) {

//    Chinese c1 = new Chinese("zhangsan",32);//This is an error because a parameter is missing, even if it is defined in advance.

    Chinese c2 = new Chinese("lisi",34,"fhai");  //Change a value 

    Chinese c3 = new Chinese("lisi",34,"China");

    System.out.println(c2.country);

    System.out.println(c3.country);

  }

}

class Chinese{

  String name;

  int id;

  static String country = "China";

  public Chinese(String name,int id,String country) {

    this.name = name;

    this.id = id;

    this.country = country;

  }

  pubic Chinese() {}

}

 

 ​

Explanation: even if we define a constructor to modify the country value in this object, it can't be modified.

(3) Summary:

I. The translation of static English is: static;

ii.static is the modified method.

III. the variable modified by static is static;

iv. all static decorated elements are called static and can be accessed by "class name" or "reference" (but not recommended).

Source code:

D43_other_anouncements_of_interviewing_object.java

D43_2_static_keyword1.java

D43_2_static_keyword2.java

Address:

https://github.com/ruigege66/Java/blob/master/D43_other_anouncements_of_interviewing_object.java

https://github.com/ruigege66/Java/blob/master/D43_2_static_keyword1.java

https://github.com/ruigege66/Java/blob/master/D43_2_static_keyword2.java

2.CSDN: https://blog.csdn.net/weixin_

3. Blog Park: https://www.cnblogs.com/ruige0000/

4. Welcome to pay attention to wechat public number: Fourier transform, personal public number, which is only used for learning and communication. The background replies to "gift pack" to obtain big data learning materials.

 

Topics: Java Attribute github Big Data