Java Constructors (constructors in abstract classes) and loading

Source: Internet
Author: User

Blog Category:
    • Principles and concepts of object-oriented design

1. Java constructors and initialization blocks:

A. Constructors for abstract classes

If a constructor that is written with arguments is displayed in the parent class (that is, an abstract class), in the subclass it is necessary to write a constructor to invoke the constructor of the parent class

Abstract class Person {//defines an abstraction that must be inherited

person (int i) {

}

}

public class Student extends person {

Student () {

Super (int i)://must display the calling parent class construction method//super represents the parent class object

}

}

B. Constructors

public class sample{

First: In this a=1

static int A

Second: a=2

static{a=2;}

Third: a=4

static{a=4;}

public static void Main (string[] args) ... {

IV: A=4, after execution a=5

a++;

V: a=5

System.out.println ("a=" +a);

}

}

Description of the class loading and life cycle of the user in the introduction section:

When the class is not used, nothing is loaded in advance,

(1) Once a class is used (import), its static variables are loaded first, then static initialization blocks, and static methods (not executed when not invoked).

(2) When this class is to be constructed with an object (new), it is created by new, or when the object is generated with reflection, its member part is loaded. is a member variable (that is, a non-static variable), a non-static initialization block, a non-static method, and finally the constructor (not executed when the instance is not created).

The member part, except the method, has a copy of each object, in which the non-static method does not have one for each object, but rather all objects share a copy, which should be noted.

Once the static section is loaded, it is not destroyed until the program ends and the virtual machine is shut down.

Non-static, that is, what is owned by each object, when the object is not referenced, the class ends his life cycle.

However, it also needs to reside in memory for a period of time, waiting for the garbage processor to clear it.

After the reference is lost, the time before the garbage is processed, although it resides in memory, it cannot be referenced again.

Note the load order described above:

The static variable is loaded first, then the static initialization block, and finally the static method.

In order to verify this order, the above code is changed slightly, adding a static method.

As follows

Package test1;

public class Classloadtest

{

static int a=2;

static{A=3;}

static{a=4;}

static void Init () {a=10;}

/** * @param args

*/

public static void Main (string[] args)

{

System.out.println ("a=" +a);//+ is a hyphen

}

}

The result output is: a=4

1. Constructors

(1) Any class, whether abstract or specific, has a constructor that, even if the programmer does not type it, provides a default parameterless constructor. The constructor must have the same name as the class, the constructor cannot have a return type, and it is important to remember that void is also a return type!

If no constructors are created in the class, the default constructor will be used, and if the programmer defines a constructor, the default constructor will not exist!

public class Book {

Private String ID;

Private String title;

Private String author;

We define a constructor ourselves

Public book (String idin,string titlein,string authorin) {

Id=idin;

Title=titlein;

Author=authorin;

}

Public String toString () {

Return "The info of the book:\n" +

"Title:" +title+ "\ n" +

"Author:" +author+ "\ n";

}

}

public class Test {

public static void Main (String[]args) {

Book Book=new book (); Compile error will occur using the default constructor

Book Book=new book ("0101001", "Thinking in Java", "Bruce Eckel");

SYSTEM.OUT.PRINTLN (book);

}

}

(2) How the constructor is executed:

The constructor for its superclass is called first, and the superclass constructor calls its superclass constructor until it reaches the object constructor, and then the object () constructor executes until all constructors complete

public class Animal {

Public Animal () {

System.out.println ("This is the animal constructor");

}

}

public class Snake extends animal{

Public Snake () {

System.out.println ("This is Snake constructor");

}

}

public class Cobra extends snake{

Public Cobra () {

System.out.println ("This is the Cobra constructor");

}

}

public class Test {

public static void Main (String[]args) {

New Cobra ();

}

}

Execution Result:

This is the animal constructor

This is snake constructor

This is the Cobra constructor

(3) The default constructor is a non-variable constructor, implicitly containing a call to super ()

If the superclass of a subclass does not have a parameterless constructor, its subclasses must implement constructors instead of calling the default constructor

public class Rpg {

private int HP;

private int MP;

private int grade;

private int exp;

Public Rpg (int hpin,int mpin,int gradein,int expin) {

Hp=hpin;

Mp=mpin;

Grade=gradein;

Exp=expin;

}

}

public class Magician extends rpg{

Public Magician () {The default constructor cannot be used!}

//}

Public Magician (int hpin,int mpin,int gradein,int expin) {

Super (Hpin,mpin,gradein,expin);

}

}

(4) Constructors can be overloaded, if one constructor in the same class needs to call another overloaded constructor, you can use this (), the variable list of this () determines which specific constructor to call

Note: this () and super () must appear in the first row of the constructor!!! and the This () and super () functions cannot be in the same constructor!!!

The constructor of an abstract class is called when it instantiates a specific subclass

The interface is not a constructor!

2. Initialize THE BLOCK:

There are three places to perform operations in a Java class:

constructors, methods, and initialization blocks

The Java initialization block consists of static initialization blocks and instance initialization blocks:

When a class is first loaded, a static initialization block runs once, and each time a new instance is created, an instance initialization block is run, and multiple initialization blocks are allowed in the class, and they are executed in the same order as they appear in the code (default from top to bottom when the program executes)

The overall order of execution: static initialization block->super (), instance initialization block, and other parts of the constructor, using an example to illustrate:

public class Father {

Public Father () {

System.out.println ("This is Super class!");

}

}

public class Test extends father{

static{

System.out.println ("This is static block!"); /Static block

}

Public Test () {

System.out.println ("This is Test constructor");//constructor

}

public static void Main (String[]args) {

System.out.println ("hello,java!");

Test test=new test ();

}

{

System.out.println ("Common init block!");

}

}

The output of the above example is:

This is static block!

hello,java!

This is super class!

Common Init block!

This is test constructor

Java Constructors (constructors in abstract classes) and loading

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.