Java Third Class Test blog

Source: Internet
Author: User

1.Java field initialization rule:

Run the code as shown below

Package demo;

public class Initializeblockdemo

{

/**

* @param args

*/

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

{

The following sentence affects the initial value of the Field field before and after the initialization block

public int field=100;

{

field=200;

}

public int field=100;

public initializeblockclass (int value) {

This.field=value;

}

Public Initializeblockclass () {

}

}

Note that this is the Java field that is placed in the public int field=100;

Before the.

Run results

If the Java field is initialized and the public int field=100;

To change the position, the original code is as follows :

Package demo;

public class Initializeblockdemo

{

/**

* @param args

*/

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

{

The following sentence affects the initial value of the Field field before and after the initialization block

public int field=100;

public int field=100;

{

field=200;

}

public initializeblockclass (int value) {

This.field=value;

}

Public Initializeblockclass () {

}

}

Run results

So The law of the Java field initialization is in order, that is, the field initialization and the local variable initialization which depends on whichever.

2. When there is an inheritance relationship between multiple classes, creating a subclass object causes the parent class to initialize the execution of the block. Static methods can inherit, and can be overridden in subclasses! Static blocks cannot be inherited. Here is the source code:

Package demo;

Class Root

{

static{

SYSTEM.OUT.PRINTLN ("Static initialization block of Root");

}

{

System.out.println ("Root's normal initialization block");

}

Public Root ()

{

SYSTEM.OUT.PRINTLN ("Root parameter-free constructor");

}

}

Class Mid extends Root

{

static{

SYSTEM.OUT.PRINTLN ("Static initialization block of mid");

}

{

SYSTEM.OUT.PRINTLN ("Normal initialization block for mid");

}

Public Mid ()

{

SYSTEM.OUT.PRINTLN ("Non-parametric constructor for mid");

}

Public Mid (String msg)

{

Calling overloaded constructors in the same class through this

This ();

System.out.println ("Mid with parametric constructor, its parameter value:" + msg);

}

}

Class Leaf extends Mid

{

static{

SYSTEM.OUT.PRINTLN ("Static initialization block of the Leaf");

}

{

SYSTEM.OUT.PRINTLN ("ordinary initialization block of the Leaf");

}

Public Leaf ()

{

A constructor that invokes a string argument in the parent class through Super

Super ("Java Initialization sequence demo");

System.out.println ("The constructor that executes the leaf");

}

}

public class Teststaticinitializeblock

{

public static void Main (string[] args)

{

New Leaf ();

}

}

Program run

From the above experiment we can know that the initialization of the parent class will be called as soon as the child class object is created. The constructor of the parent class is also called as the child class object is created. The order of the calls is to call the static initialization block, call the initialization block, and finally call the constructor. When the three blocks are called, they are done in the order in which the parent class is called before the subclass is called again.

3. Static methods access non-static variables:

Program Source code:

Establish a class to verify how static methods access non-static variables

Wang Hongwei, 2015,10,16

Package test;

public class Teststaticway

{

public static void Main (String args[])

{

Second s = new Second ();

S.show ();

}

}

Class Second

{

int A;

static void Show ()

{

Second m = new Second ();

System.out.println (M.A);

}

}

Program run

In the Java syntax, the static method is not directly accessible to non-static member variables, to access non-static member variables, can only be in the method body to create an object instance, that is, using the new keyword to create an object instance, Access to non-static member variables is accomplished through this object instance.

4. Magic Code:

5 types of shaped wrapper class byte,short,integer,long,character object, You can use a constant pool when the value is less than 127.

Source code and its comments:

Package test;

public class Strangecode

{

public static void Main (string[] args)

{

Integer i1=100;

Integer j1=100;

To take a number from a constant pool is to manipulate the same object in a constant pool. So the comparison result is true

System.out.println (I1==J1);

Integer i2=128;

Integer j2=128;

Two different objects are created in the heap space, their addresses are different, the output is false

System.out.println (I2==J2);

}

}

5. Create a class that can display the current number of objects in this class at any time

Source:

Create a class that can display the number of objects of that class at any time

Wang Hongwei, 2015,10,16

Package test;

public class Testcreatenum

{

static int num = 0;

Public Testcreatenum ()

{

num++;

System.out.println ("The number of objects currently in this class is" + num + ");

}

public static void Main (String args[])

{

Testcreatenum t = new Testcreatenum ();

}

}

Program Run Results

Programming Ideas:

Create a static constant num in the class body, to record the number of objects, the initial value is 0, and then write a constructor, the statement of the constructor is the number of output objects and num++. Constructors are automatically called as objects are created, so each object is created to show the current number of objects.

Java Third Class test blog

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.