Java classes and Objects (classroom summary)

Source: Internet
Author: User

One: The different meanings of "= ="

When "= =" is applied to the original data type variable, is the data saved by the comparison variable equal
When "= =" is applied to a reference type variable, it is compared whether the two variables refer to the same object.

The test code is as follows:

Package Test;public class QQ {public static void main (string[] args) {//TODO auto-generated method Stubfoo obj1=new Foo () ; Foo obj2=new foo (); System.out.println (OBJ1==OBJ2);}} Class Foo{int value=100;}

The result is false.

Analysis: The meaning of the example is to determine whether the two variables refer to the same object, since the example defines two different objects, so it returns false.

Two: The construction method of class

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

Package Test;public class QQ {public static void main (string[] args) {//TODO auto-generated method Stubfoo obj1=new Foo () ;}} Class Foo{int valud;public Foo (int initvalue) Value=initvalue;}

Analysis: Program error, due to custom construction method, the system no longer provides a default construction method.

Three: Execution order of static initialization blocks

Source:

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 for Mid");} {SYSTEM.OUT.PRINTLN ("Normal initialization block for mid");} Public Mid () {System.out.println ("parameter-free constructor for mid");} Public Mid (String msg) {//The constructor that is overloaded in the same class is called through this (); System.out.println ("Mid with parametric constructor, whose parameter value:" + msg);}} Class Leaf extends Mid{static{system.out.println ("static initialization block of the Leaf");} {SYSTEM.OUT.PRINTLN ("normal initialization block of the Leaf");} Public Leaf () {//The constructor super ("Java initialization sequence demonstration") that has a string parameter in the parent class is called through Super; System.out.println ("The constructor that executes the leaf");}} public class Teststaticinitializeblock{public static void Main (string[] args) {new Leaf ();}}

Results:

Analysis:

The static content of the parent class is executed first, after the static content of the parent class executes, then executes the static content of the subclass, when the static content of the subclass executes, and then the parent class has no non-static code block, if there is a non-static block of code to execute the parent class, the non-static code block of the parent class executes, When the constructor of the parent class is finished, it goes on to see if the subclass has no non-static block of code, and if there is a non-static block of code that executes the subclass. The non-static code block of the subclass executes and then executes the subclass's construction method. In short, the static code block content executes first, then executes the parent class non-static code block and construction method, then executes the subclass non-static code block and constructs the method.

Note: The constructor of a subclass, regardless of the constructor with no arguments, will first look for the parent class's constructor without parameters. If the parent class does not have a constructor with no arguments, the subclass must use the supper key to call the parent class with the constructor of the parameter, otherwise the compilation cannot pass.

Four: Accessing instance members of a class in a static method

Source:

Package Test;public class QQ {  int x = instance variable of the 3;//class, initializes a static variable with a value of 3  static int  y = 4;//class, and initializes a value of 4 public  static VO Id method ()//static method  {       System.out.println ("instance variable x =" + new QQ (). x);//Accessing the class's instance variable in a static method requires first instantiating    the class SYSTEM.OUT.PRINTLN ("static variable y =" + y);//static variable of class can be accessed directly in static method} public static   void Main (string[] args)      {          Qq.method ();         QQ ex = new QQ ();        System.out.println ("x =" + ex.x);}    }

Java classes and Objects (classroom summary)

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.