Examples of static

Seeking the number of generated objects

. 1  class A {
 2      Private  int I;
 . 3      Private  static  int cnt = 0 ; 
 . 4      // here with static modification, so cnt belong to the class, a plurality of objects share a property, reducing memory allocation 
. 5      
. 6      public A () { // when the new object is automatically performed constructor with no arguments 
. 7          CNT ++; // number of objects +1 
8      }
 . 9      
10      // constructor with no arguments and parameters have to be performed when the constructor and new objects can be performed only one 
. 11      
12 is      public A ( int I) { // new new objects and values are invoked when passed constructor parameter 
13 is          the this .i = I; 
 14         ++ CNT; // number of objects + 1'd 
15      }
 16      
. 17      PUBLIC static  int getCnt () { // a static modification, belong to the class, called directly by the class name 
18 is          return CNT; // returns the number of objects A 
19      }
 20  }
 21 is  
22 is  public  class TestStatic {
 23 is      public  static  void main (String [] args) {
 24          System.out.printf ( "A current number of objects is:% d a", A.getCnt ()); // 0 months 
25          A1 = A new new A ();
 26 is         System.out.printf ( "A current number of objects is:% d a", A.getCnt ()); // . 1 one 
27          A = A2 new new A ();
 28          System.out.printf ( "A Current Object number is:% d a ", A.getCnt ()); // 2 months 
29          A = A3 new new A ();
 30          System.out.printf (" A current number of objects is:% d a ", A .getCnt ()); // . 3 th 
31      }
 32 }

 

Restrictions: only produce an object

. 1  class A {
 2      public  int I = 20 is ;
 . 3      Private  static A = A new new A ();  
 . 4        // A both objects A class is a Class A properties! A point itself
 . 5        // static here can not be omitted, the following method will access a getA object (attributes)
 6        // otherwise static methods can not access non-static member 
. 7      
. 8      Private A () { // the constructor provided , so that it can only create the object as a private class in which the present 
. 9      }
 10      
. 11      public  static a getA () {
 12 is      // the return value of type a, is a type of address, the address of a target    
 13 is      // getA call in the other class () method, the object can be obtained a
 14     // Note that this static method must be set to static, other classes may not be able to create objects access 
15          return A;
 16      }
 . 17  }
 18 is  
. 19  public  class TestStatic {
 20 is      public  static  void main (String [] args) {
 21 is          A A1 = A.getA ();
 22 is          A = a2 A.getA ();
 23 is          A1.i = 99 ;
 24          System.out.printf ( "% D", aa2.i); // output 99, indicating that a1 and a2 is the same object
 25            // because the object address a1 and a2 are the same getA () method returns a target address 
26      }
 27 }

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/sunbr/p/11480912.html