Java's hands on the brain October 20

Source: Internet
Author: User

(1) hands -on brain

The function does not have an initial value and then if the class provides a custom constructor method, it will cause the system not to provide a default constructor.

(2)

public class Test {

/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
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 () {}
}

The result of the code execution:

The variables for all classes in the Java field are initialized to null by default, such as String A; Integer b;
The original data type of the numeric class is initialized to 0 by default, such as int A; Short B; char c;
The Boolean default is initialized to False. The default value that is specified when the class member definition is executed or the initialization module of the class exactly executes that one, to see which one is in the front row
2,: constructor for executing class
3: The initialization block of a class does not accept any arguments, and as long as an object of the transcend class, they are executed. Therefore, it is appropriate to encapsulate the code that must be executed when the object is created.
(3)
Execution Result:

The execution order of the static initialization module:
A: The static initialization block can only be executed once, and the creation of the subclass object also causes the static initialization block of the parent type to be executed once.
(3) An instance member of a class is accessed in a static method
Example:
publicclassButtonFactory {
private static ButtonFactory _instance =  null ; public static ButtonFactory getInstance() {      if ( null == _instance)      _instance =  new ButtonFactory();      return _instance; } } (4) The Paradox of integer

(5)
Using the class's static classes and constructors, we can keep track of the number of classes that a class creates. Please write a class at any time you can query how many objects you have created
Instance:

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's hands on the brain October 20

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.