Class and object after class jobs

Source: Internet
Author: User

1.

public class Initializeblockdemo {

/** * @param args * *

public static void Main (string[] args) {

Initializeblockclass obj=new Initializeblockclass ();

System.out.println (Obj.field);

Obj=new Initializeblockclass (300);

System.out.println (Obj.field);

}

}

Class initializeblockclass{

The following sentence, before and after the initialization of the block, affects the initial value of field fields//

public int field=100;

{

field=200;

}

public int field=100;

public initializeblockclass (int value)

{

This.field=value;

}

Public Initializeblockclass () {

}

}

Initialization blocks are primarily used for initialization of objects, called when an object is created, and can be used to complete initialization of property values and load other classes of functionality.
Initialization blocks and Construction methods function Similarly, you can create objects again when you complete some initialization operations, in general, the construction method initialization and initialization of block initialization can be common.


The construction method can be initialized by the parameters of the vessel, but the initialization block cannot, initialization block initialization before the construction method is executed, if the method is overloaded multiple times, you can consider the construction method to initialize the code in the initialization block.

Executes the default value specified by the class member definition or the initialization block of the class, exactly which one you want to see in front of.

2. Initialize the order of execution

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 of mid");

}

{

SYSTEM.OUT.PRINTLN ("Normal initialization block for mid");

}

Public Mid ()

{

SYSTEM.OUT.PRINTLN ("Non-parametric constructor for mid");

}

Public Mid (String msg)

{

Calling overloaded constructors in the same class through this

This ();

System.out.println ("Mid with parametric constructor, its parameter value:" + msg);

}

}

Class Leaf extends Mid

{

static{

SYSTEM.OUT.PRINTLN ("Static initialization block of the Leaf");

}

{

SYSTEM.OUT.PRINTLN ("ordinary initialization block of the Leaf");

}

Public Leaf ()

{

A constructor that invokes a string argument in the parent class through Super

Super ("Java Initialization sequence demo");

System.out.println ("The constructor that executes the leaf"); }

}

public class Teststaticinitializeblock

{

public static void Main (string[] args)

{

New Leaf ();

}

}

Conclusion:

1. Parent class static (sequential in code order)
2. Subclass static (sequential in code order)
3. Parent class non-static domain, constructor (sequential in code order)
4. Subclass non-static domain, constructor (order in code order)

3.

public class Statictest {

public static void Main (String args [])

{

Test b=new test (); B.print ();

}

}

Class Test {

int i=1;

static void print ()

{

Test a=new test ();

System.out.println (A.I);

}

}

Accessing an instance member of a class in a static method (that is, a field or method that does not have an attached static keyword) requires creating an object and accessing the instance members through the object.

4. using the class's static fields and constructors, we can track the number of objects created by a class. Write a class that can query it at any time to see how many objects you have created.

public class Test3 {
private static int i = 0;

Public Test3 ()
{
i++;
}

public static int GetNumber ()
{
return i;
}

public static void Main (string[] args) {
Test3 T1 = new Test3 ();
Test3 t2 = new Test3 ();
Test3 t3 = new Test3 ();
Test3 T4 = new Test3 ();
System.out.println ("Altogether created" + test3.getnumber () + "objects");
}
}

Results:

Class and object after class jobs

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.