There are many understandings about variables on the Internet , uneven , some good and some bad , I don't know what's right , What's wrong , So I write this article on these blogs and my own understanding , If there's something wrong , Hope readers can correct , thank .

Variable is a kind of variable we often use , I was just learning Java
When , It's also plagued by the concept of variables , I didn't dig at that time , But I'm writing a similar article , I want to give you a brief introduction of variables as a subtitle , But we found that , The concept of variable is still complicated , Let's have a deeper understanding of this article
Java The concept of these variables in

Variable summary

therefore , What are the concepts of variables ? Up to now , At present, the known variables are mainly

* Instance variable
* global variable
* Static variable
* Class variable
* local variable
* Member variable
* constant
Now let's take a break through approach to each concept

Instance variable

Instance variables are also known as Instance variables. Not used static Key definition , And in any way , Construction method , Variables other than blocks are instance variables
. Instance variables are all instance specific , Instance variables are not shared between instances , in other words , Each instance of an object has its own instance variable . Here is an example of an instance variable
class Fruits { public String fruitName; // With public access fruitName; private int
fruitNum; // With private access fruitNum; }
You can call instance variables in the following way
public class Fruits { public String fruitName; private int fruitNum; public
static void main(String[] args) { Fruits fruits = new Fruits();
fruits.fruitName = "strawberry"; fruits.fruitNum = 100; } }
How to identify instance variables

So how do I know a variable is an instance variable ? Here are some rules for defining instance variables

* Instance variables can be decorated with four access modifiers :public,protected,default,private
* Instance variables can be used transient,final Keyword modification
* Instance variable not available abstract,synchronized,strictfp,native,static Keyword modification
Instance variable with default , in other words , Instance variables can be used without initialization . Here are the initial values of common instance variables

Characteristics of instance variables

We have learned the basic characteristics of instance variables and how to distinguish them , Let's talk about the characteristics of instance variables .

*
Instance variables can only be declared in a class , But in the method , Outside constructor or any block .

*
When allocating space for objects in the heap , An area will be assigned to each instance variable .

*
Instance variables can only be used by creating objects , When using new Keyword to create an object , Instance variables are also created , When the garbage collector recycles objects , Instance variables are also destroyed .

*
Instance variables can be decorated with access modifiers

*
Instance variables do not need to be forced to initialize , It has its own default .

*
Each object has its own copy of an instance variable , So changing variables in one object does not affect instance variables in other objects

*
Instance variables can only be used by creating object references .

global variable

Global variables are also known as Global variables. If you have programming experience in other languages , such as C,C++
The words , You'll be exposed to the concept of global variables , You can use the following code to create global variables
#include<stdio.h> // global variable int A; int B; int Add() { return A + B; }
But in Java in , There is no global variable . because Java Is an object-oriented programming language , All content is part of the class .Java
The reason for this is to prevent data and class members from being intentionally or unintentionally modified by other parts of the program . So in Java in , Use static variables For global access .

Static variable

Static variables are also known as Static variables. The definition of static variable is simple , Static variables are variables belonging to this class , It was created by static Keyword to decorate .static
Decorated variables are static , It can only be defined inside a class , External to method .

Characteristics of static variables

* Static variables can only be used static Keyword modification , It cannot be declared in a method , Whether static or non-static .

* Static variables are initialized before the program runs , And initialize only once . Static variables have an initialization sequence , We said later .
* All instances of static variables share the same copy . in other words , There is only one static variable , It does not copy with the creation of an object instance
* Static variables can be passed through the class name . Variable name Visit , And you don't need to create any objects to access it . public class Fruits { public String
fruitName; private int fruitNum; static String fruitType; public static void
main(String[] args) { Fruits.fruitType = "apple"; // Class name . Variable name
System.out.println(fruitType); } }
* Can be used in non static methods Using static variables in
Class variable

Class variables are also called Class variables stay Java in , Class variables are static variables , They all use static
Keyword modification , therefore , If you hear about static variables again , It's a class variable .

local variable

There's another way of saying it , say Java Only class variables in , Instance and local variables . There's no problem with that , Someone might ask , Where did you put the member variables and constants ? Don't worry. We'll talk later

Let's talk about local variables

Local variables are also called Local variables. It means in the method , Variables defined in the constructor or block code . Life cycle following method of local variable , constructor , Code block is destroyed after execution .

No matter how some of the above variable concepts change , Local variables are all standing as loose , Sit like a clock , Walk like the wind , Recumbent as a bow , Calmly deal with the comparison of different articles . It's a really easy variable .

So we need to study the characteristics of such a good thing

* By definition , Local variable defined in method , In a constructor or code block ;
* Then the life cycle of local variables follows the method , constructor , Code block is destroyed after execution ;
* Local variables cannot use access modifiers , For example, the following code

*
Local variables are only declared in methods , Visible in constructor or block , Local variables can only call these methods , Internal use of constructors or blocks

*
Local variable has no default value , So local variables should be initialized when they are first used or declared

Member variable

what ? Member variables in Java Instance variable in ? Is that right ?

constant

Validation process

To test this conclusion , We turned to stackoverflow website

We search java member variable and instance variable That's the title for us

Roughly speaking : What is a member variable ? Are member variables and instance variables the same ? We have an answer below

Give in the first sentence , Instance variables and class variables are called member variables , Then it gives JDK Definition of variables in the official website manual . It means in Java in , There are only three types of variables

* Defining member variables in a class --- Called properties
* Defined in method ( Include construction method ) Or variables in block code --- Called a local variable
* Variables defined in method definition --- Called parameters
Um ... Maybe it's not a good solution to our problem , I asked for help again with questions JDK Official website manual , And then I saw about Variables Definition of

It means in Java in , There are only the following types of variables ( Don't talk about anything else , Are you more reliable than the official website ?)

* Instance variable ( Non static properties ) : That is to say

Non static properties are also called instance variables , Because their values are relative to each instance . let me put it another way , For each object , The values of instance variables are unique ;

*
Class variable ( Static properties ): Class variable is to use static Fields declared by modifiers , This tells the compiler : No matter how many times the class is instantiated , Only one copy of the variable exists . in addition , Keywords can be added
final To represent constants .

*
local variable : There is no special keyword to declare the specified variable as a local variable , Determining what it declares depends entirely on the location of the declared variable .

*
parameter : Think about the most commonly used method ? Certainly main Method ,main How is the method defined ?
public static void main(String[] args) {}
Of which args Is it String Array of , We also call it a parameter , So there is no keyword to declare the parameter , Identifying it as a parameter also depends only on where it is declared .

bilibili

therefore
I wrote an article to make it clear to you , Now you should know Java What are the variables in .

If you use static To define variables , Can only be a class variable , Or static variables , And its definition position can only be in the class , Outside method or code block , There is only one copy of the variable .

If you don't static To declare variables , So there are three variables

* Defined in construction method , Code block , Variables outside the method are called instance variables , The number of copies of instance variables is the same as the number of instances .
* Defined in method , Construction method , Variables within a code block are called local variables ;
* Variables defined in method parameters are called parameters .
As described in the following code
public class VariablesInJava { int instanceVariable; // Instance variable static String
staticVariable; // Class variable public void method() { String localVariable =
"localVariable"; // local variable System.out.println(localVariable); } public static
void main(String args[]) {} // parameter }

Technology
©2019-2020 Toolsou All rights reserved,
TP6 Application examples of verifier and correct verification data Unity Scene loading asynchronously ( Implementation of loading interface ) Gude Haowen serial - You deserve to be an engineer ( Preface ) Bitcoin in ten years ,VDS Opportunity or fraud Huawei certification HCIA-AI artificial intelligence Image explanation of over fitting and under fitting Python Basic knowledge and notes ESP8266/ESP32 System : Optimize system startup time First knowledge python Skills summary use C++ I want to talk to you “ Prototype mode ” ( copy / copy constructor )