Usage and Points for Attention of static in java

  • 2021-09-05 00:10:32
  • OfStack

In the use of keywords, we have already known the static method. In order to prevent some unnecessary mistakes in use, everyone should master the scope of its use. This article divides the usage points of static into two aspects, one is the access scope, and the other is the attention about method call. Let's take a look at the complete usage points of static.

1. When using static methods, only properties and methods declared by static can be accessed, while properties and methods declared by non-static cannot be accessed.


package com.jk.ref;
class People{
String name;
private static String country=" China ";
public People(String name){
this.name=name;
}
public void tell(){
System.out.println("name:"+name+" "+"country:"+country);
}
/**
 * @return the country
 */
public static String getCountry() {
return country;
}
/**
 * @param country the country to set
 */
public static void setCountry(String country) {
People.country = country;
}
}
public class StaticDemo01 {
 
public static void main(String[] args) {
// TODO Auto-generated method stub
People.setCountry("shanghai");
People ps1=new People("zhangsan");
//People.country=" Shanghai ";
ps1.tell();
People ps2=new People("lisi");
// ps2.country=" Shanghai ";
ps2.tell();
People ps3=new People("wangwu");
// ps3.country=" Shanghai ";
ps3.tell();
}
}

2. Parent class reference can only call parent class and subclass override methods, and parent-child methods with the same name will not be overwritten but obscured.


public class TestMain {
public static void main(String[] args) {
Super sup = new Sub(); // Package (upward shape) 
sup.m1(); // Parent class reference cannot tune subclass unoverridden method, output mi in Super
sup.m2();// Call subclass methods m2 Inheritance first builds the parent class method, overrides (overrides) the method with the same method name, and outputs m2 in Sub
Sub sub = (Sub)sup; // Unpacking (downward modeling) 
sub.m1(); // Calling subclass static methods m1 First, build the parent class method, the method name is the same, the method name is the same, and the output m2 in Sub
sub.m2();// Call subclass methods m2 Inheritance first builds the parent class method, overrides (overrides) the method with the same method name, and outputs m2 in Sub
}
}
class Super{ // Parent class 
public static void m1() { // Parent static method 
System.out.println( " m1 in Super " );
}
public void m2() { // Parent class method 
System.out.println( " m2 in Super " );
}
}
class Sub extends Super{ // Subclass 
public static void m1() { // Subclass static method 
System.out.println( " m1 in Sub " );
}
public void m2() { // Subclass method 
System.out.println( " m2 in Sub " );
}
}

Content extension:

In-depth summary

Quoting a netizen's words, it is very good. If others ask you the role of static; If you say statically decorate the properties of classes and the methods of classes, others think you are qualified; If you can form a static code block, others think you can still; If you say you can form a static inner class, then others think you are good; If you say static guide package, others think you are OK;;;

Then we will first summarize static in these aspects; Then talk about 1 vague place and 1 easy place to ask in the interview;

static method

static method 1 is generally called static method. Because static method can be accessed without relying on any object, there is no this for static method, because it does not depend on any object. Since there are no objects, there is no this. And because of this feature, non-static member variables and non-static member methods of the class cannot be accessed in static methods, because non-static member methods/variables must depend on concrete objects before they can be called.


Related articles: