The difference between constructors for Java and assigning values directly to member variables

Source: Internet
Author: User

First, take a look at why you use a constructor? 1. Java classmember Variablesis modified to public, protected, and packageaccess RightsAt different levels, you can directly givemember VariablesAssigned value. However, the assignment is based on the assumption that the object holding the member variable must exist. The initialization of the object must call the constructor , so the constructor is essential. As for the useconstructor FunctionOr assigning a value to a member variable directly when it is declared depends on the situation. If you create an object that is immutable, you must initialize the member variable with the constructor, and vice versa. In addition, it is not recommended to assign a value directly to a member variable, because it will destroy its encapsulation. Therefore, it is recommended to assign a value to a variable in a constructor or to provide a setters method. 2, the assignment of the member variable is generally through the constructor function; The direct assignment is usually a number of constant member variables, beginning with the final keyword.
3, the other time is basically using the constructor function constructor to try to create objects dynamically. The idea of object-based programming is that the direct assignment of variables directly to a class is not supported. Small opening
Links: http://www.zhihu.com/question/20616186/answer/15646132
Source: Know

class variables, instance variables are similar in initialization, and for instance variables there is an example
Public classramtest {
{
Price = 3.4;//①--non-static statement block
}
PublicRamtest ( DoublePrice) {
This . Price = price;//②--constructor Function
}
Double price = 2.0;//③--declaring statements and assigning values
}


In this example, the instance variable price is initialized in three places
Execution order: ③ in price variable declaration [price=0.0]
--① price variable assignment [price=3.4]
--③ price variable assignment [price=2.0]
--② value in the price constructor
It is important to note that although the assignment of the price variable in the initial block of a non-static statement precedes the declaration, the Declaration of the variable is executed first, and then the assignment of the variable value is executed in code order, and then the constructor initializes the instance.
The initialization statements of these three instance variables are processed by the compiler and are merged into the constructor, which defines the assignment statements converted by the variable statement, and the statements in the initialization block. always before all statements in the constructor。 The order in which the two assignment statements are merged keeps their order in the source code.

The difference between constructors for Java and assigning values directly to member variables

Related Article

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.