Hands on the brain

Source: Internet
Author: User

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

 Package test;  Public class Test {    publicstaticvoid  main (string[] args)    {        Foo obj1= New Foo ();    }} class foo{    int  value;      Public Foo (int  initvalue)    {        value=initvalue;    }}

A constructor in a class has a parameter that does not have parameters when defining the object, so it cannot be compiled

2. According to the output of the Code, self-Summary Java field Initialization law

 PackageInit; Public classInitializeblockdemo {/**     * @paramargs*/     Public Static voidMain (string[] args) {Initializeblockclass obj=NewInitializeblockclass ();                System.out.println (Obj.field); Obj=NewInitializeblockclass (300);    System.out.println (Obj.field); }}classinitializeblockclass{//The following sentence affects the initial value of the Field field before and after the initialization block//public int field=100;{field=200; }     Public intfield=100;  PublicInitializeblockclass (intvalue) {         This. field=value; }     PublicInitializeblockclass () {}}

Experiment:

 PackageInit; Public classInitializeblockdemo {/**     * @paramargs*/     Public Static voidMain (string[] args) {Initializeblockclass obj=NewInitializeblockclass ();                System.out.println (Obj.field); Obj=NewInitializeblockclass (300);    System.out.println (Obj.field); }}classinitializeblockclass{//The following sentence affects the initial value of the Field field before and after the initialization block     Public intfield=100; {Field=200; }    //public int field=100;     PublicInitializeblockclass (intvalue) {         This. field=value; }     PublicInitializeblockclass () {}}

Experiment:

Java is initialized in both the initialization block and the constructor method, and when the same variable is initialized multiple times, it is initialized in order and the last result is preserved. If there is no formal parameter when creating an object in the main function, if a common variable is defined in the class and assigned, then the value is assigned to the variable in the main function, the default constructor in the class is called, and the corresponding constructor in the class is called if the object is created in the main function as a physical parameter.

3. Observe the output and summarize the "sequence of execution of static initialization blocks"

Execution order:

The static initialization block of the parent class is executed first, the static initialization block of the subclass is executed, the instance of the parent class is executed in the initialization block, the constructor method, the instance of the subclass initializes the block, the constructor. Executes the static first, executing the instance and constructing the method.

4. Static methods only allow access to static data, so how do you access the instance members of the class in a static method (that is, a field or method that does not have the static keyword attached)?

 Packagetest; Public classTest { Public Static voidMain (string[] args) {Foo.s (); }}classfoo{intValue1=1; Static intvalue2=2;  Public Static voids () {System.out.println ("Value1=" +NewFoo (). value1); System.out.println ("Value2=" +value2); }}

Experiment:

When a static method accesses members of this class, only static members are allowed, and when static members of this class need to be accessed in a static method, an object of that class needs to be defined and then accessed through the method of the object name. Static members.

5. Two pairs of integers exactly the same, why one output true, one output false?

 Public class Strangeintegerbehavior {        publicstaticvoid  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);}        }

I1 and J1 Call the cache method in integer, so i1 and J1 point to the same object, so true. I2 and J2 Create new objects, pointing to different, so the result is false

Hands on the 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.