"Java Foundation" member variables and local variables

Source: Internet
Author: User

member variables and local variables

you can divide variables into member variables and local variables, depending on where you define the variables.a member variable is a variable defined within a class scopeA local variable is a variable defined within a method member variables can be divided into:instance properties (without static adornments)exists as an instance attribute existsClass Property (static decoration)as the existence of a class exists member variables do not have to be explicitly initialized, they are automatically initialized by default Local variables can be divided into:Formal parameter (formal parameters)valid throughout the methodmethod Local variables (defined within a method)Valid from the time the variable is defined to the end of the method code block local variables (defined within a code block) Valid from defining this variable to the end of the block of code  Local variables must be initialized, except for formal parameters, to specify an initial value that cannot be accessed. Example code:
 Packageobject;/*** Member variables and local variables * **/ Public classVariabletest {//member Variables     Public StaticString staticname = "Class attribute";  PublicString Nostaticname = "Instance Properties"; //member variable not initialized     Public StaticString staticname1;  PublicString nostaticname1; //Define a method     Public voidinfo () {//define a local variable I in the method//int i; //The direct output is not output, because there is no initialization//System.out.println (i); //define a local variable i and initialize        inti = 10; //Output ISystem.out.println (i); }        //defines a static method     Public Static voidinfos () {inti = 20;    System.out.println (i); }         Public Static voidMain (string[] args) {/*The first question: Is the Class property the same as the scope of the instance property? */        //you can call class properties before you create an instance, but you cannot invoke instance propertiesSystem.out.println (Variabletest.staticname);//result: Class Properties//once the object is instantiated, you can invoke the instance propertiesvariabletest VT =Newvariabletest (); System.out.println (vt.nostaticname);//Result: Instance Properties        /*---Conclusion: in member variables, the scope of a class property is a little larger than the instance property---*/System.out.println ("----------"); /*second question: Do member variables need to be explicitly initialized? */        //calling a class property without initialization directlySystem.out.println (VARIABLETEST.STATICNAME1);//Result: null//invoking an instance property without initialization with an instantiated objectSystem.out.println (VT.NOSTATICNAME1);//Result: null        /*---Conclusion: member variables are automatically implicitly initialized, assigning a default value to a variable---*/System.out.println ("----------"); /*third question: What happens if I use the instantiated object to invoke the class properties? */        //Vt.staticname; //This will be an error .//Syntax error, insert "VariableDeclarators" to complete localvariabledeclaration//translation: syntax error, inserting "variable declarator" to complete local variable declaration        /*Why did you get an error?         At first I thought it was because eclipse was wrong. * Later I rewrite a test * file directly with a text document, the report is not the wrong statement, and then I tried again *variabletest.staticname * is also an error, indicating that this writing is not correct, specifically why the need to study*/Vt.staticname= "Changed class Properties"; //If you assign a value to a class attribute at the same time, you will not get an error ... Have a warning//The static field Variable test.static name should is accessed in a static//translation: Static field variable test. Static names should be accessed in a static waySystem.out.println (Vt.staticname);//Result: Changed class properties//This will not be an error, but there is a warning, ditto ↑        /*Conclusion: It is possible to invoke a class property with an instantiated object, in a properly formatted case, but with a warning * class properties are called through the object, and the value of the class property can also be changed*/System.out.println ("----------"); //local variables defined in a method        /*The Fourth question: Defines the local variables in the method, is there a way to access it? */        //Calling MethodsVt.info ();//Results: Ten//is it possible to use the I in info now? //System.out.println (i); //error: I cannot be resolved to a variable//translation: I cannot be converted into a variable        /*Conclusion: A local variable defined in a method cannot be accessed by means of a method.*/System.out.println ("----------"); //local variables defined in a code block        /*Fifth Q: What are the local variables defined in the code block that can be accessed by the code block? */        {            intj = 11; System.out.println (j);//results: One        }        //out of the code block//System.out.println (j); //same error, same content as above        /*A local variable defined in a block of code that cannot be accessed by a block of code*/System.out.println ("----------"); //follow up: a static methodInfos ();//Results://It's still an error .//System.out.println (i);            }}

Java allows local variables and member variables to have the same name, and local variables override the values of member variables code Example:
 Packageobject;/*** Local variables override member variables **/ Public classVariablecover {/*When an instance variable has the same name as a local variable in a method, the value of the local variable overrides the instance variable*/    //Defining instance Variables     PublicString City = "Hefei"; Private StaticString citys = "Chuzhou"; //Define a method     Public voidfunction () {String city= "Bengbu"; //Output//a local variable with the same name in the method overrides the instance variableSystem.out.println (city);//results: Bengbu//To invoke an instance variable, you can use thisSystem.out.println ( This. City);//results: Hefei    }         Public Static voidMain (string[] args) {String citys= "Nanjing"; //a local variable with the same name in the method overrides the instance variableSystem.out.println (Citys);//Result: Nanjing//overridden class properties can be called through the class nameSystem.out.println (Variablecover.citys);//results: ChuzhouString City= "Shanghai"; System.out.println (city);//Results: Shanghai//This can alsoSystem.out.println (NewVariablecover (). City);//results: Hefei        Newvariablecover (). function (); }}

/* article is i from my csdn blog in the exam, because there is not accustomed to use, more chaotic, no this side purely ... Although some of the features are quite good ... After turning around, it was deserted .... */

 

"Java Foundation" member variables and local 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.