Thinking in Java: initialization and cleanup

Source: Internet
Author: User

Initialized with a constructor (a special method that is automatically called when the object is created). So how do you create an object in a variety of ways? The answer is method overloading. Method overloading involves some problems. How to differentiate overloaded methods is obviously the number of parameters, type, or even order. Also note that the return value cannot be used as one of the distinguishing methods. The second is how to choose which overloaded method to invoke. The answer is exact match with recent principles. Knowing the method overloads, we realized that we could have multiple constructors (which is also our purpose). So note that when we do not define the constructor ourselves, the compiler raises a default constructor to ensure that there is always a constructor present.

Jump to another question. How does a method know which object is being called? The This keyword----a reference to the object that called the method. Another usage of this: calls to other constructors in a constructor (must be placed at the beginning of the constructor). Once you know this, you can talk about the static method. The static method is the method without this.

Clean I can't figure it out and look back later.

Initialization issues (issues related to the order of initialization and initialization) first, the local variables of the method must be explicitly initialized.

And for the class member data, there is a good summary in the book.

When a dog's object is first created, or when a static or static member of the dog is called, the Dog.class is loaded (the class object is created), the static initialization action executes, and when new dog () allocates storage space for the dog object in the heap, the storage space is zeroed, It then executes all the initialization actions that appear at the field definition. Executes the constructor again.

Explicit static initialization vs. non-static instance initialization: Wrap the statement in curly braces.

About the initialization of the array. Sub-basic data types and non-basic data types. 1. The former can be initialized with curly braces 2. With new. The former new has an array of general meanings, and the latter is a reference array. So the real initialization of the latter is done by actually creating the object and assigning it to the reference in the array (you can use new to add curly braces).

When we use arrays as method parameters, the number of parameters is variable. Javase 5 provides a method for changing the list of parameters.

Enumeration.

Thinking in Java: initialization and cleanup

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.