Java Basic Knowledge collation (initialization and cleanup) __java

Source: Internet
Author: User
In object-oriented processes, initialization is an essential part of Java to ensure that entity objects for each class can be allocated to storage without unnecessary null-pointer exceptions.

Java Builder

In Java, the function of the initialization object is called a constructor, and each class has a default constructor with no arguments. If no other constructor with parameters is declared in the class, the compiler automatically generates a "hidden" default constructor.
Note: The constructor does not have any return type (unlike void) and the constructor itself is a static method constructor that can implement different behavior through the method overload. If a constructor is already defined, the compiler will not help you build the default constructor

Method Overload:
Unique identification of the method overload: Each overloaded method must have a unique argument list.

Attention:
The return type does not fully recognize the overloaded method, because the return value of the method may not be noticed when a user invokes a method of a different return type. Therefore, the return type is not sufficient to differentiate between overloaded methods.

this keyword
The This keyword represents a reference to the current object.

Scenario: In a member method of a class, you need to get a reference to the current object within the method, and you need the This keyword. And the **this keyword can only be used within a method, representing the object calling the method * *

 Note:
       If you call other methods of the class in this method, you do not need to use this keyword specifically, only when you call the member variable of the class in that method.
       * * Do not put this in some unnecessary places, because the code written by others does not use this everywhere, and it follows a simple and intuitive programming style

Constructor is called in the Builder:

Scenario: When there are multiple overloaded constructors in a class, you may need to use the This keyword to reduce the repetition rate of your code when you call another constructor in one of the constructors.
Public Class flower{Public
    String s;
    public Flower (int num) {
    //do something
    } public
    Flower (String s, int num) {This
        (num);
        THIS.S = s;
    }
}
    The above code indicates that the constructor flower, although it can invoke a constructor with the This keyword, cannot call two, and can only be initialized by THIS.S = S.
    Also, * * when calling other constructors in the constructor, you must place the This keyword at the very beginning, * * Otherwise it will not compile.

Cleanup: End Processing and garbage collection (overview, detailed in the JVM)

Java has a garbage collection mechanism, responsible for reclaiming memory resources occupied by unwanted objects, but there are special cases where an object (not using new) is given a "special" area of memory, and the garbage collector is only responsible for reclaiming the memory resources allocated through new, which can cause an omission.

 therefore, in the Java object class, a Finalize () method is defined.

 how he works:

     once the garbage collection is ready to release the storage space occupied by the object, the Finalize () method is invoked first, and the memory occupied by the object is actually reclaimed when the next garbage collection action occurs.

  Note: Finalize () is not equal to a destructor in C + +, because once the destructor is invoked, the object is bound to be destroyed, while the objects in Java are not garbage collected.
    in other words:
     -1 objects may not be garbage collected.
     -2 garbage collection does not equal the destructor.
     -3 garbage collection is only related to memory.

Member initialization

Java tries to ensure that all variables are properly initialized before they are used by the * *. For local variables within a method, Java implements this assurance in the form of a compile-time error.
Specifies initialization
Where you define a class member variable, you can directly assign it to an initial value.

Constructor initialization

  Can be initialized with a constructor. At run time, you can call a method or perform certain actions to determine the initial value and provide greater flexibility for weaving. However, automatic initialization cannot be prevented.
  in the following code, because only a variable i,i of type int is declared in the class, the initial value of 0 is automatically assigned before it is used, and then it becomes 7 in the constructor.
public class counter{
    int i;
    Counter () {
        i = 7;
    }
}
  -Initialization order:

     within the class, the order in which the variables are defined determines the initialization order, even if the variable definition is spread between the method definitions, before the method is called.

     -static data initialization
         Note
             -1 static data occupies only one storage area, shared by all objects
             -2 The Static keyword cannot be applied to local variables because local variables are stored on the stack and are destroyed with the end of the method call.
             -3 If a domain is a static, basic Type field, the standard initial value of the basic type is also obtained without initialization. , the default initialization value is NULL if it is a reference to an object.
             -4 * * * Static initialization is done only at the necessary time,

    such as calling a static method of a class, creating a Class object (as described earlier, the constructor is also a static method, except that the declaration static keyword is not displayed.) Or static initialization occurs the first time the static data is accessed.



 -Static initialization

     of the display Java allows multiple static initialization actions to be organized into a special static clause (or "static Block"). This clause is initialized in the same order as other static initialization actions, and is executed only once.
public class spoon{
    static int  i;
    static{
        i = n;
    }
-Non-displayed static initialization
 , a clause with no static keyword, is used to initialize a non-static variable of a class.

Note: Regardless of which clause, he takes precedence over the constructor initialization before executing.

The order of execution is: static clause-> non-static clause initialization of –> constructor array
When declaring an array, the compiler does not allow its size to be specified, only to initialize it through the form "{}".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.