When learning "Thinking in Java", encountered chapter 2 Exercise 1, the problem is that "/** create a class, it contains an int field, a char domain, they are not initialized, print their values out, verify that Java has performed the default initialization ”。 That's what I wrote about just getting this problem.
1.
package;
public class Exerciseone {
public static void Main (String [] args)
{
int A;
char c;
System.out.println (a);
System.out.println (c);
}
}
/**
The consequences are easy to think, the first time to write is not good, it became a program can not be compiled
**/
After a little modification to the program, it looks like this.
2.
package;
public class exerciseone{
static int A;
static char C;
public static void Main (String [] args)
{
System.out.println (a);
System.out.println (c);
}
}
/**** This can be run because the member variables inside the class exercise are decorated as static, because we all know that the main () method is static, so it can only access the static decorated members. The reason is that a member without a static adornment needs to create an object before it can be accessed, while a static method may not create any objects when called. **/
And about the object and reference between the description, please read the Java books themselves, I believe there will be a lot of receipts.
Finally, I looked at the standard answer, as follows:
/** Standard Answer
package;
public class exerciseone{
int i;
char c;
Public Exerciseone () {
System.out.println ("i=" +i);
System.out.println ("c=" +c);
}
public static void Main (String []args) {
New Exerciseone ();
}
}**/
The key to the standard answer is to generate an instance object to "send a message" to the class Exerciseone int and char member variables, saying I want to call you, and then print out the value.
Oneexerciseone creates a class that contains an int field, a char field, they are not initialized, and verifies that Java performs the default initialization