Differences between java and c # object initialization

Source: Internet
Author: User

First, let's take a look at c #

1 class Program
2 {
3 static void Main (string [] args)
4 {
5 new Child ();
6}
7}
8
9
10 class Faher
11 {
12 public static Test FSTest = new Test ("base class static member ");
13
14 public Test FTest = new Test ("base class instance Member ");
15
16 static Faher ()
17 {
18 Console. WriteLine ("base class static constructor ");
19}
20
21 public Faher ()
22 {
23 Console. WriteLine ("base class constructor ");
24}
25
26}
27
28 class Child: Faher
29 {
30 public static Test CSTest = new Test ("subclass static member ");
31
32 public Test CTest = new Test ("subclass instance Member ");
33
34 static Child ()
35 {
36 Console. WriteLine ("subclass static constructor ");
37}
38
39 public Child ()
40 {
41 Console. WriteLine ("subclass constructor ");
42}
43}
44
45
46 // class used as a member
47 class Test
48 {
49 public Test (string info)
50 {
51 Console. WriteLine (info );
52}
53}

Program result:

Static member of subclass
Subclass static Constructor
Subclass instance Member
Base Class static member
Base Class static Constructor
Base class instance Member
Base Class Constructor
Subclass Constructor

Let's take a look at java's

1 public class Show
2 {
3 public static void main (String [] args)
4 {
5 new Child ();
6}
7}
8
9 class Father
10 {
11 public static Test FSTest = new Test ("base class static member ");
12
13 public Test FTest = new Test ("base class instance Member ");
14
15 public Father ()
16 {
17 System. out. println ("base class constructor ");
18}
19
20 static
21 {
22 System. out. println ("base class static block ");
23}
24}
25
26 class Child extends Father
27 {
28 public static Test CSTest = new Test ("subclass static member ");
29
30 public Test CTest = new Test ("subclass instance Member ");
31
32 public Child ()
33 {
34 System. out. println ("subclass constructor ");
35}
36
37 static
38 {
39 System. out. println ("subclass static block ");
40}
41
42}
43
44
45 // class used as a member
46 class Test
47 {
48 public Test (String info)
49 {
50 System. out. println (info );
51}
52}

Program result:

Base Class static member
Base Static Block
Static member of subclass
Subclass static Block
Base class instance Member
Base Class Constructor
Subclass instance Member
Subclass Constructor

As shown in the preceding procedure

C # The initialization order is

Static subclass member --> static subclass constructor --> subclass instance Member --> base class static member --> base class static constructor --> base class instance Member --> base class constructor --> subclass Constructor

Java initialization order is

Static base class members --> static base class blocks --> static subclass members --> static subclass blocks --> base class instance members --> base class constructor --> subclass instance members --> subclass constructor

 

Summary:

C # when creating an object, the static members of the base class will be initialized first for each member of the subclass and then for each member of the base class (while the static members always come before the instance members ), when creating an object in java, the base class static members and subclass static members are always initialized first, and then the base class instance members and subclass instance members are created, c # and java finally call the base class constructor and then call the subclass constructor.

Key points:

1 c # first child base, java first base and then child

2.

3. First base and then sub-structure

 

 

Related Article

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.