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? )