This chapter explains the creation of objects to the process of being recycled, and describes the life cycle of objects
Heap and Stack (stack)instance variable: an instance variable is only declared under a class, outside of the method (the default value of the instance variable is 0/0.0/false, the default value of the reference is null)local variable: A variable declared in a method, or a parameter in a method. is also a stack variable For example:Public class test{int size;Public void Foof (int a) {int b;barf () }Public void Barf () {Duck D =new Duck (); }}in the example above,size is an instance variable, and each instance will have one;A and B are local variablesD is also a local variable, but a non-primitive (int, etc) variable is an object reference variable. Placed on the
heap: instance variables are placed on the heap, and all objects are on the heap. Java is allocated according to the required space of the instance variable (primitive is allocated according to the size, and the referenced variable is placed on the heap if it is instantiated, if not just a reference amount)
placed on stack: local variables, called methods. The currently called method is displayed at the top of the stack (the newly called method is inserted from the top of the stack). For example: The above example we go to callTest a = new test ();A.foo () the heap and stack scenarios are as follows:
constructor Function
constructor FunctionThe program code that executes when the New keyword is used creates an instance of the object, all classes have a construct with data, and if not written the compiler writes a default constructor (the compiler will not help create the constructor if you build the constructor yourself)Public class duck{Public Duck () { }} The constructor name is the same as the class name, and there is no return type. constructors can be used to initialize the state of an object
Overloaded ConstructorsA class can have more than one constructor, but the number or type of arguments must be different (regardless of the parameter name), just like overloading, called overloaded constructors For example, the overloaded constructors for the following example are reasonablePublic class mushroom{Public mushroom () {}Public mushroom (int size,boolean ismagic) {}Public Mushroom (boolean ismagic,int size) {}}
the constructor of the parent classThe subclass object contains the instance variables of itself and the parent class, and when the object of the subclass is created, all inherited constructors are executed when the constructor executes, the first thing to do is to execute the constructor of its parent class, which is called the "constructor chain" until it is chained to the class object. So the end result is a layer of calls from the object to the next parent class (stack, advanced post-out) the subclass calls the constructor of the parent class using Super (), and if not added, the compiler automatically adds super (), super () to the
first row of the subclass constructorIf you call a constructor with a parameter to the parent class, it is included in the constructor of the subclass and is called with super (parameter) . The following is a method that invokes a parent class constructor with parameters and parameters. Of course the call without parameters does not write super () also can, the compiler will automatically addPublic class Hippo extends animal{Public Hippo () {Super () }Public Hippo (String name) {Super (name) }} This
()This refers to the object itself, which can only be used in the constructor and placed on the
first line , so this () and super () can only select oneUse this () to call another constructor in the same class from a constructor For example, the following example:class Mini extends car{color color;Public min () {This (color.red)//Actual call to public Mini (Color C) Constructor }Public min (Color c);Super ("Mini")color=c;}
the life cycle of an objectThe life cycle of an object depends on the reference it references, and if the reference is still alive, when the last reference disappears it becomes recyclable Local variables: The scope of a local variable in a method is only in the method in which
It is declared, the local variable is reclaimed after the method call is complete (the method executes is ejected from the stack), and when method A calls method B, the local variable in method a pauses to save the value, and the activity resumes when the B callback is completed. Reference
variable: The reference variable is the same as a local variable and can be referenced only when it is in scope, referencing a local variable
Instance variable: The scope of the instance variable is the class to which it belongs, and if the object is alive, the instance variable is also alive, and if the object is freed, it is freed (the instance variable and the object live on the heap)
three ways to dispose of objects:1. Place the reference variable in the method, and it will be released after the method is finished.void Go () {Life z=new Life) ()} 2. Re-assign new objectsLife z=new Life ();z=new Life ();//The first object can be recycled after Z has been assigned a value 3. Assigning a reference directly to nullLife z= New Life ();Z=null; Note: If object A is referenced by other object B, it cannot be freed by A=null or by re-allocating memory to a, because the fast memory is also occupied by B
Java basic four [constructors and garbage collection] (read head first Java Records)