Java classes and objects hands-on brain

Source: Internet
Author: User

Hands on the brain 1

Why is the following code not compiled? Where did it go wrong?

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

Hands on the Brain 2

To test

    • Using the classes defined on the previous page slide, what is the output of the following code?

Public class Test

{

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 ()

{

}

}

Experimental results:

When you use a constructor definition to automatically call the appropriate constructor based on the number and type of arguments, you also perform the assignment of the global variable when you call the class's initialization function with a global variable.

Initialization Order of class fields

    1. Executes the default value specified by the class member definition or the initialization block of the class, exactly which one you want to see in front of.
    2. Executes the constructor of the class.

The initialization blocks of the class do not receive any arguments, and they are executed whenever an object of the class is created. Therefore, it is appropriate to encapsulate those "code that must be executed when the object is created."

Hands on the brain 3

Run the Teststaticinitializeblock.java sample, observe the output, and summarize the "Order of execution of static initialization blocks".

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 ();

}

}

Execution order of static initialization blocks

    1. Static initialization blocks are executed only once.
    2. When you create an object of a subtype, it also causes the execution of the static initialization block of the parent type.

Hands on the Brain 4

An interesting question.

    • Static methods only allow access to static data, so how to access the instance members of a class in a static method (that is, there is no additional Static field or method of the keyword) ?
    • Please write code to verify your idea.

public class Buttonfactory

{

private static buttonfactory _instance = null;

public static Buttonfactory getinstance ()

{

if (null = = _instance)

_instance = new Buttonfactory ();

return _instance;

}

}

Hands on the brain 5

Integer How does a class's boxing and unpacking actually happen?

public class Boxandunbox

{

public static void Main (string[] args)

{

int value=100;

Integer Obj=value; Packing

int result=obj*2; Unpacking

}

}

search for "boxing" and "Unpacking" Secrets ( learn )

    • Use JAVAP Anti-compilation class file

Disassembly Tool JAVAP The parameter Description

Hands on the brain 6

Integer of the " Bizarre " features "

Public class Strangeintegerbehavior

{

Public Static void Main (string[] args)

{

Integer i1=100;

Integer j1=100;

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

Integer i2=129;

Integer j2=129;

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

}

}

Use JAVAP to analyze the build class file and see what it calls up Interger class, and then open the JDK source file to view the source code, you can find the answer.

After-school assignments 1

Using the class's static fields and constructors, we can track the number of objects created by a class. Write a class that can query it at any time "how many objects have you created?" ”。

Public class Objictnumber

{

int number;

Public Objictnumber ()

{

++number;

System. out. println ("Currently created" +number+ "objects");

}

Public Static void Main (String[]args)

{

@SuppressWarnings ("unused")

Objictnumber obj=New objictnumber ();

}

}

Java classes and objects hands-on brain

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.