In Java, the scope of a variable is divided into four levels: class-level, object-instance-level, method-level, block-level.
Class-level variables, also known as global-level variables or static variables, need to be decorated with the static keyword, which you can learn from in contrast to static variables in C + +. Class-level variables already exist after the class is defined, occupy memory space, can be accessed through the class name, and do not need to be instantiated.
An object instance-level variable is a member variable that is not allocated until the memory space is instantiated for access.
A method-level variable is a variable defined inside a method, which is a local variable.
A block-level variable is a variable that is defined inside a block, the lifetime of the variable is the block, and the block disappears, such as the IF and for statements. A block is a code surrounded by curly braces, for example:
1 {2int age = ten; 3 String name = "Www.cnblogs.com"; 4 // correct, the age and name variables can be accessed inside the block 5 System.out.println (name + "already" + Age + "old"); 6 }7// error, cannot access the age and name variables outside the block 8 System.out.println (name + "already" + Age + "years old");
Description
- In addition to accessing the method-level variables inside the method, you can access the class-level and instance-level variables.
- The inside of a block can access class-level, instance-level variables, and if the block is contained inside a method, it can also access the method-level variables.
- Method-level and block-level variables must be initialized to be displayed, otherwise they cannot be accessed.
Demo Code:
1 Public classdemo{2 Public StaticString name = "Blog Park";//class-level variables3 Public intI//object instance-level variables4 //property block, which runs when the class initializes the property5 {6 intj = 2;//block-level variables7 }8 Public voidtest1 () {9 intj = 3;//Method-level variablesTen if(J = = 3) { One intK = 5;//block-level variables A } - //block-level variables cannot be accessed here, and block-level variables can only be accessed within blocks -System.out.println ("name=" + name + ", i=" + i + ", j=" +j); the } - Public Static voidMain (string[] args) { - //do not create objects, access class-level variables directly from the class name - System.out.println (demo.name); + //How to create an object and access it -Demo T =NewDemo (); + T.test1 (); A } at}
Operation Result:
Micro Science Court
Name= Blog Park, I=0, j=3
Series Articles:
Java know how much (1) Language overview
Java know how much (2) virtual machine (JVM) and cross-platform principle
Java know how much (3) employment direction
Java know how much (4) the difference between J2SE, Java EE, J2ME
Java know how much (5) Build Java development environment
Java know how much (6) The first example of a program
Java knows how many (7) classes and objects
Java know how much (8) class library and its organizational structure
Java know how much (9) Import and Java class search path
Java know how much (10) data types and variables
Java know how much (11) data type conversions
Java know how many (12) operators
Java know how much (13) Process Control
Java know how many (14) arrays
Java know how much (15) string
Java know how much (StringBuffer) and Stringbuider
Java know how much (17) emphasize the programming style
Java know how many (18) classes are defined and instantiated
Java know how many (19) access modifiers (access control characters)
Java knows how many (20) variables are scoped
Java knows how many (20) variables are scoped