Oneexerciseone creates a class that contains an int field, a char domain, they are not initialized, and verifies that Java performs the default initialization

Source: Internet
Author: User

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

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.