Java programmer must read: Basic (6)

Source: Internet
Author: User
Tags constructor contains variables variable
Program | programmer Java programmer must read: Basic articlesTime: 2001/09/13 13:31 Author: ZSC Pacific Network College

 
2.4.2 initialization instances and class members

The following is an example of initializing instances and class members:

You can use the static initializer and the instance initializer to provide initialization values for class and instance members when you define them in the class:

Class Bedandbreakfast {

static final int max_capacity = 10;

Boolean full = false;

}

This is not a problem for the original data type. Sometimes, it can be used to create arrays and objects. But this initialization form has its limitations, as follows:


The initialization program can only perform initialization that is expressed with an assignment statement.


The initialization program cannot invoke any method that contains an exception error.


If the initialization program calls a method that contains an exception error, it cannot perform an error recovery.

If you have some initialization to complete, some may not be implemented in the initialization program because there is one of the above limitations, at which point you have to place the initialization code at will. To initialize the class member, place the initialization code in the static initialization block. In order to initialize an instance member, the initialization code is placed in the constructor.
2.4.3 Static initialization block

Let's talk about static initialization blocks.

Here's an example, as shown in Figure 8:


(Figure 8)


The Errorstrings source code must be initialized in the static initialization block. This is because error recovery must be performed only when the source code is not found. At the same time, Errorstrings is a class member that cannot be initialized in a constructor. In the previous example, a static initialization block starts with the static keyword, and the Java code is enclosed in braces "{}".

A class can have many static initialization blocks, which can appear anywhere in the class. The system guarantees that the static output block and the static initialization program are invoked in the order in which they are in the source code.

2.4.4 Initialize Instance members

If you want to initialize an instance variable and cannot handle it at the variable declaration, you can only initialize the class in the constructor. If Errorstrings is an instance variable rather than a class variable, you can initialize it by using the following code:

Import Java.util.ResourceBundle;

Class Errors {

ResourceBundle errorstrings;

Errors () {

try {

Errorstrings = ResourceBundle.

Getbundle ("errorstrings");

catch (Java.util.MissingResourceException e) {

Error Recovery code here

}

}

}

Now the code initializes the errorstrings for the class in the constructor.

Sometimes, a class contains many constructors and each constructor allows the caller to provide initialization values for different instance variables of the new object. For example, Java.awt.Rectangle has the following three constructors:

Rectangle ();

Rectangle (int width, int height);

Rectangle (int x, int y, int width, int height);

The Rectangle () constructor does not have any arguments, so it cannot provide initialization values for the user size or origin and size, while the other two constructors allow the user to set the initial value.

However, all instance variables (origin and size) must be initialized. In this example, the class often has a constructor to complete all the initialization. Other constructors call this constructor and provide it with parameters or default values. For example, here are the three constructors described above, which are initialized as follows:

Rectangle () {

This (0,0,0,0);

}

Rectangle (int width, int height) {

This (0,0,width,height);

}

Rectangle (int x, int y, int width, int height) {

this.x = x;

This.y = y;

This.width = width;

This.height = height;

}

Java language Support instance initialization block, you can use it with ease. Constructors are recommended for initialization here for the following three reasons:


All the initialization code is in one place, making the code easier to maintain and read.


The default value can be cleared to know.


Constructors are widely known to Java program designers, including relatively new Java program designers, and instance initializers cannot, and he can cause confusion among other Java programmers.



2.4.5 Objects and classes
You may notice that objects and classes look very similar. In the real world, the distinction between classes and objects is often a source of confusion for programmers. In the real world, it is clear that classes cannot be the objects they describe. However, it is difficult to differentiate between classes and objects in software. Part of the reason is that software objects are only electronic models in the real world or abstract concepts. But also because objects are often sometimes referred to as classes and instances.

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.