7k7k pen questions (2)

Source: Internet
Author: User

It mainly includes knowledge about static, local variables, and instance variables.

Public class demo {

Static string STR [] = new string [5];

Public static void main (string ARGs []) {

System. Out. println (STR [1]);

}

}

Running result: NULL

Explanation:Static string STR [] = new string [5]; indicates a static attribute of the class. It does not belong to an object, belongs to a class, and is also an array of instance variables, if no initial value is assigned to the instance variable, the system automatically assigns the initial value to the instance variable: the initial value of int type is 0, the initial value of boolean type is false, and the initial value of Class Object type is null.

Differences between instance variables and local variables:

1. location: local variables are defined in the method, and instance variables are defined outside the class.
2. scope of use: local variables can only be used in the method defined by them. You can call the variable name directly.
instance variables can be used within the class at least, objects must be called for use
3. local variables must be initialized before they are used, and instance variables do not need to be assigned an initial value. The system will give the default initial value.
4. local variables cannot be duplicated in the same method. Local variables and instance variables can be duplicated. the proximity principle is used in the method. The local variables prevail. You can use this. instance variables call instance variables.

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.