Java Job 02

Source: Internet
Author: User

One, class practice

(i) Construction method

1, source code

public class test{

public static void Main (string[] args) {

Foo obj1=new foo ();

}

}

Class foo{

int value;

Public Foo (int initvalue) {

Value=initvalue;

}

}

2, running results

3, results analysis

If a class provides a custom construction method, the default construction method is no longer provided by the system.

(ii) Java field initialization

1, source code

public class Initializeblockdemo {

public static void Main (string[] args) {

Initializeblockclass obj=new Initializeblockclass ();

System.out.println (Obj.field);

Obj=new Initializeblockclass (300);

System.out.println (Obj.field);

}

}

Class initializeblockclass{

{

field=200;

}

public int field=100;

public initializeblockclass (int value) {

This.field=value;

}

Public Initializeblockclass () {

}

}

2, running results

3, results analysis

Off with Java field initialization problem, no inheritance: Initialize block first execution, field initializer set code second execution, constructor third execution.

(iii) Initialization issues when there is an inheritance situation

1, source code

Class b{

Public B () {

System.out.println ("Fourth execution of the parent constructor");

}

{

System.out.println ("The third execution of the parent dynamic code block");

}

static {

System.out.println ("The first execution of the parent class static Block");

}

}

public class A extends b{

Public A () {

System.out.println ("sub-class constructor sixth execution");

}

{

System.out.println ("Subclass dynamic code block fifth execution");

}

static {

System.out.println ("sub-class static block second execution");

}

public static void Main (string[] args) {

New A ();

}

}

2 Running Results

3 Results Analysis

The parent class static blocks the first execution, the subclass static blocks the second execution, the parent class the initial value code executes, the parent class constructor fourth executes, the subclass dynamically initializes the block fifth execution, the subclass constructor sixth executes.

When there is an inheritance relationship between multiple classes, creating a subclass object causes the parent class to initialize the execution of the block.

(iv) Results of operation:

Summary analysis:

Object Initialization order: first executes the parent class static content, after the parent class static content executes, then executes the subclass static content, when the subclass static content executes completes, then goes to see the parent class has not the non-static code block, if has executes the non-static code block of the parent class, the parent class's non-static code block executes, The constructor of the parent class is then executed, and after the construction method of the parent class is executed, it goes on to see if the subclass has no non-static block of code, and if there is a non-static block of code that executes the subclass. The non-static code block of the subclass executes and then executes the subclass's construction method. In short, the static code block content executes first, then executes the parent class non-static code block and construction method, then executes the subclass non-static code block and constructs the method.

Second, after-school homework

Write a class that can be queried at any time "how many objects have you created

(a) Source code:

public class Test {
private int id;
private String name;
private static Long Count;
Private final static ThreadLocal tl=new ThreadLocal ();

Public Test () {
Super ();
count++;
Tl.set (count);
}
Public long GetCount () {
Return (Long) tl.get ();
}
public static void Main (string[] args) {
for (int i = 0; i < 5; i++) {
Test t=new test ();
System.out.println (T.getcount ());
}
}

}

(ii) The results of the operation

Java Job 02

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.