static

package Test;
/*Static features:
*1. Loaded with the class loading
*2. Preceding the existence of the object
*3. Shared by all objects of the class, if a member variable is shared by all objects, then It can be set as a static variable, the common use is static, and the characteristic is non-static
*4. Static members can be called with the class name
*5. There is no this keyword in static methods (because static is loaded with the loading of the class Yes, this exists with the creation of the object, static exists before the object)
*6. Static can only access static, not non-static, because non-static can only be used after the object is created
*/

public class Static {

  public static void main(String[] args) {
    PersonOne p=new PersonOne();
    p.name="kobe";
    p.country="USA";
    p.speak();//kobe,USA

    PersonOne p1=new PersonOne();
    p1.name="kevin";
    p1.speak();//kevin,USA,country have been assigned before
  }

}
class PersonOne{
  String name;
  static String country;//The static representation is the public part
  public void speak() {
    System.out.println(name+","+country);
  }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324979744&siteId=291194637