Initialization and cleanup of Java programming ideas

Ensure the initialization constructor : by the constructors, ensure that each object is initialized. The name of the class constructor with the same name. Allocate space when you create an object, and call the appropriate constructor initialized. In java, initialize and create bundled together, it can not be separated. Constructor is a special method, there is no return value.

Method overloading : different forms of the same name parameter method, each overloaded method to distinguish by the formal parameter list. When passed actual parameter is less than the type of the formal parameter, automatically converts the actual data type is promoted (for char types will be converted to type int). If the parameter is greater than the actual form of the incoming parameters, you need to be displayed narrowing conversion.

The default constructor (default, no-argument) : When the writing is not a class constructor, the compiler automatically created. If there is argument constructor defined, you must manually define the constructor with no arguments.

this keyword : the compiler secretly "references the operation target" as the first parameter passed to the method. If you want to get inside the method reference to the current object is calling the method, it is with this, this method can only be used internally. If you need to call another method in the method of the same class, the this need not, be called directly.

this can also be constructed in the configuration wherein the call. A constructor can only call one other constructor, and to put the very beginning, otherwise an error. Can only be called in the constructor must not call the constructor with this in other places.

This is understood by the static, non-static method can not be called from within a static method, which in turn can. With general static class called directly, you do not need to create an object. Into static methods in a class you can access other static fields, and static methods.

// other methods within the class may not call this 
public  class Apricot {
 void Pick () {} ......
 void PIT () {Pick (); ......) 
} 

// with this representation of the current referenced object 
public  class Leaf {
     int I = 0 ; 
    Leaf INCREMENT () { 
        I ++ ; 
         return the this ; 
    } 
    void Print () {System.out.println ( "I =" + I);}
     public  static  void main ( String [] args) { 
         Leaf X = new new Leaf (); 
         ... x.increment () INCREMENT () INCREMENT () Print ();  // I =. 3 
    } 
} 

// the this object to other current methods 
 class the Person {
      public  void EAT (the Apple Apple) { 
         the Apple Peeled = apple.getPeeled (); 
         of System.out.print ( "the Yummy" ); 
     } 
} 
class Peeler {
     static the Apple Peel (the Apple Apple) {
         // Remove Peel 
        return Apple; // Peeled 
    } 
} 
class the Apple { 
    the Apple getPeeled () { 
        return Peeler.peel ( this ); // use this method to transfer itself to the external
    }
}
public class PassingThis {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Person().eat(new Apple());
    }//Yummy
}
//this调用构造器
import static net.mindview.util.Print.*;
public class Flower {
    int petalCount=0;
    String s="initial value";
    Flower(int petals){
        petalCount=petals;
        print("Constructor w/ int arg only,petalCount= "+petalCount);
    }
    Flower(String ss){
        print("Constructor w/ String arg only,s="+ss);
        s=ss;
    }
    Flower(String s,int petals){
        the this (Petals); // can only be called one, and be placed in the beginning of 
        the this .s = s; // before s is a member, after a s is the parameter 
        Print ( "int & String args" ); 
    } 
    Flower ( ) { 
        the this ( "Hi", 47 ); 
        Print ( "default comstructor (NO args)" ); 
    } 
    void printPetalCount () { 
        Print ( "petalCount =" + petalCount + "S =" + S); 
    } 
    public  static  void main (String [] args) { 
        Flower X = new new Flower (); 
        x.printPetalCount (); 
    } 
} / *Output:
Constructor w/ int arg only,petalCount= 47
String & int args
default comstructor(no args)
petalCount=47S=hi
*/

 

Cleanup : termination and garbage collection : For special new memory area is not obtained, because the garbage collector reclaims the memory only new allocation, in this case defined in the class allows finalize () method is recovered. finalize () is called first, the next time garbage collection occurs, will really recovered memory. Common finalize () to find some flaws, the following demonstrate it.

Object may not be garbage; garbage collection is not equal to "destructor"; garbage collection only memory related

class Book {
    boolean checkedOut=false;
    Book(boolean checkOut){
        checkedOut=checkOut;
    }
    void checkIn() {
        checkedOut=false;
    }
    protected void finalize() {
        if(checkedOut) {
            System.out.print("Error:checked out");
        }
    }
}
public class TerminationCondition {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Book novel =new Book(true);
        //proper cleanup
        novel.checkIn();
        //drop the reference,forget to clean up
        new Book(true);
        //force garbage collection&finalization
        System.gc();    //Error:checked out
    }
}

End of this case that: all book objects are checkIn before being gc, but one is not due to negligence on checkIn recovered, it will print out the error. With finalize () to verify this end conditions.

Member initialization : Method in the local variables must be initialized, and class fields can be initialized by default to 0, null, false.

Constructor initialization : static initialization performed only at the necessary time, carried out the first time was created, can not be initialized again.

Object Creation: 1 Find a class path, locate the .class file
          2 .class load, static initialization
       3 When the new object when the heap memory space allocated for the object at
       4 this storage space is cleared, automatically all the basic types of objects set to the default value of
       5 appears to perform all field definitions initializing operation at
       6 executes constructor

// order (with particular attention to static initialization is performed only once) the occurrence of various initialization 
// The instance initialization will be executed each time it is invoked
class Bowl { Bowl ( int marker) { System.out.println ( "Bowl (" + marker + " ) " ); } void F1 ( int marker) { System.out.println ( " F1 ( "+ marker +") " ); } } class the Table { static Bowl bowl1 = new new Bowl (. 1 ); // ---- . 1 the Table () { // -----. 3 System.out.println ( "the Table ()" ); bowl2.f1 ( . 1 ); } void f2(int marker) { System.out.println("f2("+marker+")"); } static Bowl bowl2=new Bowl(2);//-----2 } class Cupboard { Bowl bowl3=new Bowl(3); //------3 static Bowl bowl4=new Bowl(4);//------1 Cupboard(){ //------4 System.out.println("Cupboard()"); bowl4.f1(2); } static void f3(int marker) { System.out.println("f3("+marker+")"); } static Bowl bowl5=new Bowl(5);//-------2 } import static net.mindview.util.Print.*; public class StaticInitialization { public static void main(String[] args) { print("Creating new Cupboard() in main");//3 new Cupboard();//4,不再静态初始化 print("Creating new Cupboard() in main"); new Cupboard(); table.f2(1); Cupboard.f3(1); } static Table table= new Table();//1 static Cupboard cupboard=new Cupboard();//2 }/*output: Bowl(1) Bowl(2) Table() f1(1)- Bowl(4) Bowl(5) Bowl(3) Cupboard() f1(2) Creating new Cupboard() in main Bowl(3) Cupboard() f1(2) Creating new Cupboard() in main Bowl(3) Cupboard() f1(2) f2(1) f3(1) */

 

Array initialization : array-valued just assigned a reference to an array. There is a fixed length array members, can not be modified. For reference types array or a reference to the array after the new, until you create a new object and assign a reference, to complete the initialization process. Use a null reference error.

void printArray static (Object ... args) {   // a variable argument list
  for (Object obj: args)
    Print (obj + "";
  the println ();
}

Guess you like

Origin www.cnblogs.com/yh-queen/p/11179149.html
Recommended