Recent revision of JAVA related knowledge, in the book appeared a variety of variable types such as: member variables, local variables, static variables, and so on. I feel a little bit dazzling, once again collate the knowledge of variables, if there are errors, also ask you to put forward.
On Variables
In JAVA, the description of variables can only be divided into member variables and local variables.
Membership variables
Class variables, also known as class variables, are directly defined outside the class or method. Generally, no initialization is required. At compilation time, the system will automatically initialize (usually initialize at class loading, prior to method).
Membership variables can generally be divided into instance attributes and class attributes (static variables). The difference between them is that class attributes (static variables) need static modification when declared, while instance attributes do not need static modification. In usage, static variables can be invoked before an instance is created, and instance variables must be invoked after an instance is created.
local variable
A local variable is a variable defined within a method, a code segment, or a formal parameter declared within a method.
Local variables can generally be divided into three categories:
Formal parameters: valid throughout the method
Method Local Variables (Method Definition): Valid from the beginning of defining this variable to the end of the method
Code Block Local Variables (Definitions in Code Blocks): Valid for a period from the beginning of defining this variable to the end of the code block
For local variables, it must be explicitly initialized (given an initial value), otherwise it cannot be accessed directly.
A code demonstration of an older generation is cited later.
package object; /** * Membership variables and local variables * * */ public class VariableTest { //Membership variables public static String staticname = "Class attributes"; public String nostaticname = "Instance attributes"; //Uninitialized member variables public static String staticname1; public String nostaticname1; //Define a method public void info(){ //Define a local variable i in the method //int i; //Direct output is not output because it is not initialized. //System.out.println(i); //Define a local variable i and initialize it int i = 10; //Output i System.out.println(i); } //Define a static method public static void infos(){ int i = 20; System.out.println(i); } public static void main(String[] args) { /*First question: Are class attributes the same as instance attributes in scope?*/ //Class attributes can be invoked before an instance is created, but instance attributes cannot be invoked. System.out.println(VariableTest.staticname);//Result: Class attributes //After instantiating the object, you can call the instance properties. VariableTest vt = new VariableTest(); System.out.println(vt.nostaticname);//Result: Instance attributes /*--- Conclusion: In member variables, the scope of class attributes is a little larger than that of instance attributes.*/ System.out.println("----------"); /*Second question: Do member variables need explicit initialization?*/ //Direct invocation of uninitialized class properties System.out.println(VariableTest.staticname1);//Result: null //Calling uninitialized instance attributes with instantiated objects System.out.println(vt.nostaticname1);//Result: null /*--- CONCLUSION: Membership variables are automatically implicitly initialized and assigned a default value to the variables - -*/ System.out.println("----------"); /*Question 3: What happens if class attributes are invoked with instantiated objects?*/ //vt.staticname; //This will lead to errors. //Syntax error, insert "VariableDeclarators" to complete LocalVariableDeclaration //Syntax error, insert "variable declarer" to complete local variable declaration /*Why did you make a mistake? At first I thought it was because eclipse had made a mistake. *Later, I rewrote a test directly from a text document. *After compiling the file, the report was not a statement error, and then I tried again. *VariableTest.staticname *It is also an error report, which shows that this writing is incorrect and why it needs to be studied.*/ vt.staticname = "Changed class attributes"; //If you assign class attributes at the same time, there will be no error.. Warning //The static field Variable Test.static name should be accessed in a static way //Translation: Static field variable test. Static names should be accessed in a static method System.out.println(vt.staticname);//Result: Changing class attributes //That way, there will be no mistakes, but there will be warnings, ibid. /*CONCLUSION: Class attributes can be invoked with instantiated objects in the correct format, but there are warnings. *By calling class attributes on objects, you can also change the value of class attributes.*/ System.out.println("----------"); //Local variables defined in methods /*Fourth question: Define the local variables in the method. Can the method be accessed?*/ //Call method vt.info();//Result: 10 //Can i still be used in info now? //System.out.println(i); //Error: i cannot be resolved to a variable //Translation: i cannot be converted into a variable /*CONCLUSION: Local variables defined in the method can not be accessed if the method is given.*/ System.out.println("----------"); //Local variables defined in code blocks /*Fifth question: Local variables defined in the code block, out of the code block can be accessed?*/ { int j = 11; System.out.println(j);//Results: 11. } //Out of the code block //System.out.println(j); //Same error, same content as above /*Local variables defined in a block of code are inaccessible once the block is out*/ System.out.println("----------"); //Follow-up: A Static Method infos();//Results:20 //It still makes mistakes. //System.out.println(i); } }
Although the knowledge of searching variables on the Internet can find a large number of, but their own collation (study the notes of the Big Guy) once, the impression is more profound 2333 333.
2019.07.26