Java journey (6) initialization and cleaning

Source: Internet
Author: User

Use constructors to ensure initialization. Java object creation and initialization are the same concept.

 

Methods To distinguish between overloaded Methods: A list of unique parameter types

Constructor: If there is no constructor, the compiler will help you create a constructor without parameters. However, if you construct a constructor

No matter what you construct, the compiler will no longer help you.

 

Usage of this: this applies only to those cases, And you specify the special circumstances for using the reference of the current object. For example

Return Statement to return the reference of the current object.

/////////////////////////////////////

Public class testthis

{

Int I = 0;

Testthis increment ()

{

I ++;

Return this;

}

 

Void print ()

{

System. Out. println ("I =" + I );

}

 

Public static void main (string [] ARGs)

{

Testthis x = new testthis ();

X. Increment. Print (); // I = 3

}

 

}

/////////////////////////////////////////

 

The compiler won't allow you to call constructor In the method, but you can call constructor In the constructor. You can use this

.

The parameter name is the same as the name of the data member of the class (sometimes deliberately). You can use this. X to indicate that the parameter is

Data member, not a parameter.

 

Static usage: Is it true of "Oop"? Leave it to the theory.

 

Garbage collection and finalize () are unreliable. It is also a problem to use their location and timing. Do not care too much. One thing you need to know

System. GC () can be forced to execute finalize ();

 

Member initialization: the basic data type. If it is a local variable defined within the method, no initialization will cause an error. If it is a class data

If the parameter is not initialized, the compiler automatically performs negative initial values.

The following is a summary of the basic object creation. Let's use dog as an example:

1. Create the Dog class object for the first time (the constructor is actually a static method), or access the static method or

Field, the Java interpreter will search for classpath and find dog. Class.

2. After dog. Class is loaded, all static data will be initialized. In this way, when the first class object is loaded

Static member initialization.

3. When new () is used to create a new object, the creation process of the dog object first allocates enough memory for the object in the heap.

4. This fast memory is cleared first, so that the default value is automatically assigned to the primitive type members of the dog object (for digits, the value is zero, or the corresponding Boolean and char values ), set Reference to null.

5. Perform initialization when defining member data.

6. Execute the constructor.

 

OK. Here we are today. Tomorrow we will learn "hidden implementations" and "reusable classes"

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.