Click to enter _ more _java thousand questions
What is the difference between a local variable, a class variable, and an instance variable
Before we talk about the difference between local variables, class variables, and instance variables, we need to look at Java variables.
1, what is the Java variable in the mathematical world, we know that there are constants, variables.
For example, small red and small blue everyone has brought money, small blue with 5 yuan, little red money than small blue more than 10 yuan. For such a scenario, we generally use equations to express:
Set Little red money for X, small blue money for Y, where x,y are numbers, get the equation:
y=5;
x=y+10;
Let's take a look at this process, first assume 2 x, y two variables, and then write out the 2 equations that are assigned X and y by logic, and there are 5, 102 constants in the equation.
In the program world, there are also constants, variables.
As with the mathematical world, we will first declare variables (that is, the hypothetical variable in mathematics, the difference is that you need to specify the type, do not occupy memory), and then assign a value to the variable as needed (this will allocate memory). The example above is written in a program that says:
int y=5;//
int x;
x=y+10;
Looking at this example, you can sum up the definition of a variable: derived from mathematics, is the abstract concept that can store computational results or represent values in computer language.
In Java, variables are roughly grouped into the following 4 categories: class variables, constants, instance variables, local variables
public class Test {public
static int classvar;//class variable, static variable, keyword static public
static final int constant = 10;// Constant, keyword static,final, is a special class variable that must be assigned the public
int instancevar;//instance variable public
void Dotest () {
int partvar ;//local variable
}
}
After we understand the concept of variables, we look at the difference between local variables, class variables, instance variables, and constants in Java.
2. What is a class variable (static variable): as in the example above, class variables are declared in the class with the static keyword, outside of any method, constructor, or block of code.
Class variables are created when the program is started, and are destroyed when the program stops. Class variables are stored in static memory.
Class variables are accessed by calling the class name. Such as:
public void Dotest () {
test.classvar;
}
Class variables have nothing to do with objects, whether or not you create objects, how many objects are created, and a class variable always has one copy.
3, the constant is what: the constant is marked as the final keyword class variables, class variables have the attribute constants, and again lists the difference with the class variable.
As in the example above, you must assign a value when declaring a constant, and you cannot change its value after assigning it.
Constant names generally require uppercase.
Class variables are stored in constant memory.
4. What is an instance variable:
As in the example above, an instance variable is declared in a class, outside of a method, constructor, or block of code.
Instance variables are created when an object is "new" and destroyed when the object is destroyed.
Instance variables are part of an object and are stored in the heap as objects.
Instance variables are accessed through objects. If the method provided to the external object uses:
public void Dotest () {
test test = new test ();
test.instancevar;//need variables to be public to use for external objects
}
If you give the object your own method to use:
public void Dotest () {
this.instancevar;
instancevar;//can also be used without this directly
}
5. What are local variables: as in the example above, local variables are declared in methods, constructors, or blocks of code.
Local variables are created when they enter a method, constructor, or block and are destroyed when they exit.
Local variables are implemented inside the stack level.
Local variables can be used directly in the method:
public void Dotest () {
int partvar;//local variable
partvar = 1 + 1;
}
Click to enter Ooppookid's blog