Java Job 03 (Hands-on brain and after-school assignments 1. Using the class defined below, what is the output of the following code? )

Source: Internet
Author: User

1. Using the class defined below, what is the output of the following code?

Output Result:

Please summarize the rules of Java field initialization according to the output of the code .

There are two places in Java initialization: initialization blocks and constructors, where initialization is divided into instance initialization blocks and static initialization blocks, instance initialization blocks have no keyword adornments, and static initialization blocks are modified by static .

The default value or class initialization block that is specified when the class member definition is executed, which one to see is "in front"

The constructor of the class executes: the initialization block of the class does not receive any arguments, and they are executed whenever an object of the class is created. Therefore, it is appropriate to encapsulate those "code that must be executed when the object is created."

2. When there is an inheritance relationship between multiple classes, creating a subclass object causes the parent class to initialize the execution of the block. Please write your own sample code to verify the above conclusions

Example code:

public class Jicheng {

public static void Main (string[] args) {

Son a=new son ();

A.show ();

}

}

Class father{

String name;

{

Name= "Father";

}

}

Class Son extends father{

int age=20;

void Show ()

{

System.out.println ("name" +name);

System.out.println ("Age" +age);

}

}

Results:

3. Static methods only allow access to static data, so how do you access the instance members of the class in a static method (that is, a field or methodthat does not have the static keyword attached)? please write code to verify your idea.

Code

Class exercise{

int a=0;

static int b;

static public int Getb (int newb)

{

B=NEWB;

System.out.println (b);

return b;

}

public int num ()

{

int a=1;

return A;

}

}

public class Shi {

public static void Main (string[] args) {

Exercise e=new Exercise ();

E.GETB (10);

E.num ();

}

}

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 "how many objects have you created?" ”

Code:

Class jishu{

private static int A;

Public Jishu ()

{

a++;

}

public static int Get ()

{

return A;

}

}

public class Duixiangnum {

public static void Main (string[] args) {

Jishu j1=new Jishu ();

Jishu j2=new Jishu ();

System.out.println ("Number of objects created:" +jishu.get ());

}

}

Java Job 03 (Hands-on brain and after-school assignments 1. Using the class defined below, what is the output of the following code? )

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.