Java4android initialization of classes and objects

Source: Internet
Author: User

1, Member initialization

Java strives to ensure that all variables are properly initialized before they are used.

1) Local variables of the method. Java implements this assurance with compile-time errors. eg

void F () {    int i;    i++; Error, I not initialized}
2) The data members of the class. If they are basic types, they will have an initial value, and if it is an object reference, then the reference will be initialized to null.

Specify initialization

What do I do if I want to assign a value to a variable?

1) Assign a value directly where the class member variable is defined (Note that C + + is not possible, even though beginners in C + + always want to)

Class Depth{}public class initialvalues{    boolean bool = true;    char ch = ' x ';    int i = 999;    Depth d = new Depth ();}
2) Call a method to provide the initial value.

public class Methodinit{int i = f (); int f () {return 11;}}
This function can also take parameters, but it must be an initialized parameter.

public class Methodinit2{int i = f (); int j = g (i); int f () {    return 11;} int g (int n) {   return n*10;}}
But the following is a really wrong notation.

public class methodinit3{//Int j = g (i);//illegal forward Referenceint i = f (); int f () {return 11;} int g (int n) {return n*10;}}
Clearly, the correctness of the above procedure depends onOrder of initialization。 The compiler will issue a "forward reference" warning.

The above is initialized in the defined place, which is simple and quick, so that each object created will have the same value.

2, constructor initialization

In C + + is called a constructor, here we call the constructor, all a thing. The automatic initialization described in the previous section is bound to be executed and will be executed before the constructor.

public class Counter{int I; Counter () {i = 8;}}
The above code, I will first be set to 0, and then become 7.

The initialization order is within the class, and the order in which the variables are defined determines the order in which they are initialized. Even if the variable definitions are scattered between the method definitions, they will still be initialized before any method (including the constructor) is called.
Class Window{window (int marker) {print ("window (" +marker+ ")");}} Class House{window w1 = new Window (1); House () {print ("house ()"); W3 = new Window (33);} Window W2 = New Window (2), void F () {print ("F ()");} Window W3 = new window (3);} public class Orderofinitialization{public static void Main (string[] args) {House    H = new House ();   H.f ();}} /**output**//*window (1) window (2) window (3) House () window (+) f () */
Initialization of static data
Class Bowl{bowl (int marker) {print ("Bowl (" +marker+ ")");} void F1 (int marker) {print ("F1 (" +marker+ ")");}} Class table{static Bowl bowl1 = new Bowl (1);    Table () {Print ("table ()"); BOWL2.F1 (1);} void F2 (int marker) {print ("F2 (" +marker+ ")");} static Bowl bowl2 = new Bowl (2);} Class Cupboard{bowl bowl3 = new Bowl (3), static Bowl Bowl4 = new Bowl (4);    Cupboard () {print ("cupboard ()"); BOWL4.F1 (2);} void F3 (int marker) {print ("F3 (" +marker+ ")");} static Bowl bowl5 = new Bowl (5);} public class Staticinitialization{public static void Main (string[] args) {print ("Creating new Cupboard () in main"); new Cup Board ();p rint ("Creating New Cupboard () in main"); new cupboard (); Table.f2 (1); cupboard.f3 (1);} Static Table table = new Table (), static cupboard cupboard = new cupboard ();} /**output*//*bowl (1) Bowl (2) Table () F1 (1) Bowl (4) Bowl (5) Bowl (3) cupboard () F1 (2) Creating new cupboard in Mainbowl (3) Cupboard () F1 (2) Creating new cupboard in Mainbowl (3) cupboard () F1 (2) F2 (1) F3 (1) */
Be sure to look at the above code and results carefully. It's best to run it all by yourself. The order of initialization is the first static object, then the non-static object. Static initialization occurs only at the necessary moment. If you do not create a Table object and do not reference table.b1,table.b2, then static bowl B1 and B2 will never be created. The following steps will explain why. The object creation process summarizes the process of object creation. If there is a class named Dog: 1) Even if you do not explicitly use the static keyword, the constructor is actually a static method. Therefore, when you first create an object of type dog (a constructor can be considered a static method), or a static method of the dog class, the Java interpreter must look for the path to locate the Dog.class file when the static domain is first accessed. 2) then load Dog.class (this will create a class object), and all actions on static initialization will be performed. Therefore, static initialization is done only once when the class object is first loaded. 3) When you create an object with new dog (), you will first allocate enough storage space on the heap for the Dog object. 4) This storage space will be zeroed out. This automatically sets all the basic type data in the dog object to the default value, and the reference is set to NULL. (Again, the local variables inside the method are not initialized, and if an uninitialized local variable is used, the compilation error is prompted). 5) Perform all initialization actions that appear at the field definition. 6) Execute the constructor.
Explicit static initialization (static block) Java allows multiple static initialization actions to be organized into a special "static clause" (also called a static block).
public class Spoon{static int I;static {    i = 47;}}
The code above is the same as the static initialization action, and the code executes only once at the necessary moment.
Class Cup{cup (int marker) {    print ("Cup (" +marker+ ")");} void f (int marker) {    print ("F (" +marker+ ")");}} Class Cups{static Cup Cup1;static Cup cup2;static {    cup1 = new Cup (1);    cup2 = new Cup (2);} Cups () {    print ("cups ()");}} public class explicitstatic{public    static void Main (string[] args) {     print ("Inside main ()");     CUPS.CUP1.F (+//(1)}    //static cups cups1 = new cups ()//(2)    //static cups cups2 = new cups ();//(3)}/**output*/ /*inside Main () Cup (1) Cup (2) F (99) */
The code above either runs (1) or (1) notes the Run (2), and the static initialization of cups is executed. (3) You can also open the look, it doesn't matter, because the static initialization action only once.
A non-static instance initializes it after the static initialization, before the constructor, before we have described this step. Let's look at a piece of code:
Class Mug{mug (int marker) {    print ("Mug (" +marker+ ")");} void f (int marker) {    print ("F (" +marker+ ")");}} public class Mugs{mug Mug1; Mug Mug2; {    mug1 = new Mug (1);    MUG2 = new Mug (2);    printf ("Mug1 & Mug2 Initialized");} Mugs () {    print ("Mugs ()");} Mugs (int i) {    print ("Mugs (int)");} public static void Main (String[],args) {    print ("Inside main ()");    New Mugs ();    Print ("New mugs () completed");    New Mugs (1);    Print ("New mugs (1) completed");}} /** output*//*inside Main () Mug (1) Mug (2) Mug1 & Mug2 initializedmugs () New Mugs () Completedmug (1) Mug (2) MUG1 & MUG2 initializedmugs (int) New Mugs (1) completed*/

This syntax is necessary to support the initialization of anonymous inner classes. Tired. Learn to be here today.









Java4android initialization of classes and objects

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.