Java learning day17- keyword static

First, the keyword static

  When we write a class, in fact, in the description of the properties and behavior of its object, and the object did not produce substantial, only produce new objects by performing keyword, then the system will allocate memory space for the object, its The method can be used for external calls only.

  Sometimes we want the case regardless of whether an object or objects and no matter how many, only one copy of certain data, this time on the need for static variables in memory.

Package Penalty for Day14; 

public  class Chinese {
     static String Country; // . instantiate the class variables do not directly name the class name attribute can be used, is part of a class, instantiate an object is shared by all of this class, also known as static variables 
    name String; // instance variables, only after instantiated to use part of belonging to instantiate the object can not be shared 
    int Age; 
after adding // static, after no longer need to instantiate an object can be a 
}
package day14;

public class Test {
    public static void main(String[] args){
        
//        Chinese c = new Chinese();
//        c.country = "中国";
//        c.name = "007";
//        c.age = 17;
//        Chinese c1 = new Chinese();
//        c1.country = "中国";
//        c1.name = "5172";
//        c1.age = 18;
//        Chinese c2 = new Chinese();
//        c2.country = "中国";
//        c2.name = "250";
//        c2.age = 25;
//         System.out.println (c.country);
 //         System.out.println (c1.country);
 //         System.out.println (c2.country);
 //         // question: Is there a way to write only a nationality, so that all objects use the same nationality? 
        
        Chinese.country = "Chinese"; // After adding static, can be accessed by class name attribute. 
        
        Chinese c = new new Chinese (); 
        c.name = "007" ; 
        c.age = 17 ; 
        
        Chinese c1 = new new Chinese () ; 
        c1.name = "5172" ; 
        c1.age = 18 is ; 
        
        Chinese C2= new Chinese();
        c2.name = "250";
        c2.age = 25;
        
        System.out.println(Chinese.country);
    }
}

Second, the class properties, class methods of design ideas

  Class attribute as a variable shared between the various objects of the class. In the design of the class, the class attribute analysis which does not vary due to different objects, these properties to the class attributes, methods corresponding to the class method.

  If the caller has nothing to do with the method, such a method is usually declared as class methods, there is no need to create an object you can call class methods, thus simplifying the method call.

Package Penalty for Day14; 
// do not want some method by which different objects frequently by way of new objects to invoke methods, methods to write static 
public  class Chinese {
     public  static  void the Test () { // design a static method 
        System.out.println ( "this is a static method" ); 
    } 
}
Package Day14; 

public  class the Test {
     public  static  void main (String [] args) { 
                Chinese.test (); // . do not need to instantiate an object, the method is called direct method by the class name 
    } 
}

Print result: This is a static method

 

  Class method, that is, static methods, tools to do more

Package Day14; 

public  class Utils {
         // determines if a string is a null string 
    public  static  Boolean isEmpty (String S) {
         Boolean In Flag = to false ;
         IF (! S = null && s.equals ( "")!) { / / if s is neither a null, is not empty, it may be a string s Analyzing 
        In Flag = to true ; 
        } 
        return In Flag; 
    } 
}
package day14;

public class Test {
    public static void main(String[] args){
        String s = "001";
        System.out.println(Utils.isEmpty(s));
    }
}

Print the result is: true

 

Third, use the keyword static and Membership Features

1. Scope: in Java classes, static modification of available properties, methods, code blocks, inner class

2. is a member of the static modification has the following characteristics:

  ① As the loaded classes and loading (after the class is loaded, the static method or property can be used by the class name. Method class name or attribute)

  ② precedence over object exists (no new will be able to use)

  ③ modified member, is shared by all objects

  ④ When access allows, do not create an object, class calls directly

 

Fourth, class variables

  Class variable (class attributes) shared by all instances of the class. (This may be instantiated objects share all the properties should be careful to use, as long as a change, the change can get all classes)

Package Day14; 

public  class Chinese {
     // needs, wants to know how many new objects Chinese 
    public Chinese () { 
        Chinese.count + =. 1 ; 
    } 
    
    public  static  int COUNT; 

        public  static  void showCount () { 
            System.out.println ( "the new total" + Chinese.count + "objects" ); 
    } 
}
package day14;

public class Test {
    public static void main(String[] args){
            Chinese c1 = new Chinese();
        Chinese c2 = new Chinese();
        Chinese c3 = new Chinese();
        Chinese c4 = new Chinese();
        Chinese c5 = new Chinese();
        
        Chinese.showCount();        
    }
}

Print result: a total of five new objects

 

Fifth, class methods

1. Example no object, the class name may be used. Name of the method () designated by the access elements of static.

2. In the internal static methods can only access static properties of the class, can not access non-static properties of the class.

3. Because the example does not need to access a static method, the internal static methods can not have this. (The same can not have a super, will complain)

The method overloaded need to be static or non-static.

 

Guess you like

Origin www.cnblogs.com/su-peng/p/12512165.html