Variables in the Java class

Source: Internet
Author: User

As we all know, there are three kinds of variables in the Java class, so what is the difference between the three kinds of variables?

1. Local variables: This is a variable defined within a method. This means that the scope of the variable is within the method. The variable cannot be called in other methods.

2. Member variables: This is a variable that is defined inside the class but outside of the method, and can be instantiated when the object is created. Scopes are internal to the entire class. (including all methods)

3. Class variables: Like member variables, they are defined outside the inner method body of the class. But the difference is that this is a static variable, and plainly it has to be modified with static. Typically used with the class name + variable name, you can not create an object. Greatly improves the efficiency of memory usage.

Next, let's explain the common errors in these three variables.

Local variables cannot be public, private, protected, static, as the name implies that the scope of local variables is within the method, there is no need to use the access modifier. As for the inability to use static, it may be known to the small partners of the JVM that variables inside methods in Java are created by means of stacks. The execution of the method ends and the variables are automatically destroyed. This is inconsistent with static variables.

Variables in the Java class

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.