Hands-on brain (10.17)

1.

Why is the following code not compile? Where is wrong?

Since finding out Foo (), it is impossible to compile. Wrong: wrong in the bottom of the Foo class has been defined, after the definition of Foo () has parameters, but in the wrong place in Foo () no parameters, resulting in an error

2.

package classtext1;

public class homework {
    {
        field=200;    
        }
public int     field = 100;
public homework(int value) {
    this.field=value;
}
public homework() {
    
}

    
public static void main(String[] args) {
    homework obj = new homework();
    System.out.println(obj.field);
    obj=new homework(300);
    System.out.println(obj.field);
}
}

Compile the results:

 

 The default value is defined in the function to be executed anyone who is standing in the front. While the Senate constructor is called Who will be executed.

3.

 

 

Static initialization block is executed only once, and create an object subtype perform static initialization block when its parent type.

4.

Static method only allows access to static data, then, how in a static method access instance members of the class (ie no additional field or method static keyword)?

public class Satic {
public static void Satic1(Satic n) {
    n.Satic2();
}
public static void Satic2() {
    System.out.println("hello world");
}
public static void main(String[] args) {
    Satic b = new Satic();
    Satic1(b);

}
}

The two pairs of integers is obviously exactly the same, why the output of a true, one output false?

The results output strange, why not?

Integer object class only between -128 and 127 do cache, after (Integer) == 127 (Integer) on both sides of the packing 127, the actual point to the same object in the heap memory, (Integer) 129 == (Integer) 129 , packing after a reference type, it does not do caching, pointing to the heap of different objects, so the comparison is false. The source code

 

 As can be seen more clearly.

Static fields and constructors 6. Use class, a class can track the number of objects created. Please write a class at any time to query it. "You have created a number of objects?"

public  class Satic {
     public  static  int SUM = 0 ;
 public  static  void Satic1 (n-Satic) {
     ++ SUM; 
    n.Satic2 (); 
} 
public  static  void Satic2 () { 
    System.out.println ( "you have created" + sum + "objects" ); 
} 
public  static  void main (String [] args) { 
    Satic B = new new Satic (); 
    Satic1 (B); 

} 
}

 

Guess you like

Origin www.cnblogs.com/yangxionghao/p/11691984.html
Recommended