"Java Programming Ideas - Chapter 5 (Initialization and Cleanup)"

initialization and cleanup

5.1 Ensure Initialization with Constructors

Assuming that each class has an initizlize() method, this method reminds you to call the initizlize() method before using the object, but the user must remember to call this method himself. In Java, by providing a constructor, a class designer can ensure that every object is initialized.

To let the compiler know which method to call, the constructor has the same name as the class.
A constructor that takes no arguments is called a default constructor .
A constructor is a special type of method because it does not return a value.

5.2 Method overloading

The name of the constructor is determined by the class name, so there can only be one constructor name. So what if you want to create an object in multiple ways?

In order to allow constructors with the same method name but different parameters to exist at the same time, method overloading must be used.

Distinguish overloaded methods: each overloaded method must have a unique list of parameter types.

5.3 Default constructor

The default constructor has no formal parameters - it creates a default object.

If you write a class without a constructor, the compiler will automatically create a default constructor for you.
If you define a constructor, the compiler will not automatically create a default constructor.

5.4 this keyword

The this keyword can only be used inside a method and represents a reference to "the object on which the method was invoked".

/**
 * this返回当前对象
 * @author Administrator
 *
 */
public class Leaf {

    int i = 0;
    Leaf increment(){
        i++;
        return this;
    }

    void print(){
        System.out.println("i= "+i);
    }
    public static void main(String[] args) {
        Leaf leaf = new Leaf();
        leaf.increment().increment().print();
    }
}

1) Call the constructor in the constructor:

public class Flower {
    int petalCount = 0;
    String s = "initial value";

    public Flower(int petals) {
        petalCount = petals;
        System.out.println("Constructor w/ int arg only,"
                + "petalCount = "+ petalCount);
    }

    public Flower(String ss) {
        System.out.println("Constructor w/ int arg only,"
                + "s = "+ ss);
        s = ss;
    }
    public Flower(String s,int petals) {
        this(petals);
        //this(s) 不能调用两次
        this.s = s;
        System.out.println("String & int args");
    }
    public Flower() {
        this("hi",47);
        System.out.println("default constructor ,no args");
    }
    void printPetalCount(){
        System.out.println("PetalCount = "+petalCount +" s" +s);
    }
    public static void main(String[] args) {
        Flower flower = new Flower();
        flower.printPetalCount();
    }
}

The output is:

Constructor w/ int arg only,petalCount = 47
String & int args
default constructor ,no args
PetalCount = 47 shi

Although you can use this to call one constructor, you cannot call two. In addition, the constructor must be placed at the very beginning, otherwise the compiler will report an error.
Since the parameter s and the data member s have the same name, ambiguity is easy to occur, which can be this.s = s;solved with.

2) The meaning of static A
static method is a method without this.
A non-static method cannot be called inside a static method. (You can pass an object reference, and then by reference, you can call non-static methods and non-static data members)

5.5 Final Disposal and Garbage Collection

  1. Objects may not be garbage collected.
  2. Garbage collection does not equal "destruction"
  3. Garbage collection is only about objects.

Garbage collector technology:
reference counting technology. adaptive technology.

5.6 Member initialization

Data default value:
write picture description here

If an object reference is not initialized, the reference acquires a special reference null;
it can be assigned an initial value when defining a member variable.

5.7 Constructor initialization

Inside a class, the order in which variables are defined determines the order of initialization. Variables are initialized before other methods (including constructors) are called.

public class OrderOfInitialValue {

    public static void main(String[] args) {
        House house = new House();
//      house.f();
    }

}
class Window{
     Window(int maker) {
        System.out.println("window("+maker+")");
    }
}
class House {
    Window w1 =  new Window(1);
     House() {
        System.out.println("House()");
        w3 =  new Window(33);
    }
     Window w2 =  new Window(2);
     void f(){
         System.out.println("f()");
     }
     Window w3 =  new Window(3);
}

The output is:

window(1)
window(2)
window(3)
House()
window(33)

No matter how many objects are created, static data occupies only one copy of the storage area.
Static objects are initialized first, and then "non-static" objects are initialized.
Static code block: Like other static initialization, the code block is executed only once.

5.8 Array initialization

An array is a sequence of objects or primitive data types of the same type encapsulated together with an identifier name.

Arrays define either int [ ] a or int a [ ]. All you have now is a reference to the array, and no storage is allocated for the array itself.
Array initialization method:

int [] a  = {1,2,3,4,5,6};
Integer[] a  = new Integer[10];
Integer[] a  = new Integer[]{
            1,2,3,4,5
        };

Variable arguments: Object… args

5.9 Enumeration Types

Declare an enum class:

public enum Spiciness {
    NOT,MILD,MEDIUM,HOT,FLAMING
}

The enum class is combined with switch as the condition of the switch.

Guess you like

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