In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you what are the types of variables in Java, I hope you will gain something after reading this article, let's discuss it together!
1. Local variables
Variables defined in a method or statement block are called local variables. Variable declaration and initialization are in the method, and after the method ends, the variable is automatically destroyed.
Local variables are declared in methods, constructors, or statement blocks
Local variables are created when methods, constructors, or statement blocks are executed, and when they are completed, the variables are destroyed.
Access modifiers cannot be used for local variables
A local variable is visible only in the method, constructor, or statement block that declares it.
Local variables are assigned on the stack.
Local variables have no default values, so after local variables are declared, they must be initialized before they can be used.
2. Member variables (instance variables)
A member variable is a variable defined in a class outside the body of a method. This variable is instantiated when the object is created. Member variables can be accessed by methods, constructors, and statement blocks of a specific class.
Instance variables are declared in a class, but outside methods, constructors, and statement blocks
When an object is instantiated, the value of each instance variable is determined.
Instance variables are created when the object is created and destroyed when the object is destroyed
The value of the instance variable should be referenced by at least one method, constructor, or statement block, so that the external can obtain the instance variable information in these ways.
Instance variables can be declared before or after use
Access modifiers can modify instance variables
Instance variables are visible to methods, constructors, or statement blocks in a class. In general, you should set the instance variable to private. You can make instance variables visible to subclasses by using access modifiers
Instance variables have default values. The default value for numeric variables is 0, for Boolean variables is false, and for reference type variables is null. The value of a variable can be specified at declaration or in the constructor
Instance variables can be accessed directly through the variable name. But in static methods and other classes, you should use the fully qualified name: ObejectReference.VariableName.
3. Class variables
Class variables are also declared in the class, outside the method body, but must be declared as a static type.
Class variables, also known as static variables, are declared with the static keyword in the class, but must be outside the method.
No matter how many objects a class creates, the class has only one copy of the class variable.
Static variables are rarely used except when declared as constants. Constants are variables declared as public / private, final, and static. The constant cannot be changed after initialization.
Static variables are stored in static storage. It is often declared as a constant and variables are rarely declared using static alone.
Static variables are created the first time they are accessed and destroyed at the end of the program.
Has similar visibility to instance variables. However, in order to be visible to the consumers of the class, most static variables are declared as public types.
The default value is similar to the instance variable. The default value for numeric variables is 0, for Boolean variables is false, and for reference types is null. The value of a variable can be specified at declaration time or in the constructor. In addition, static variables can be initialized in static statement blocks.
Static variables can be accessed through: ClassName.VariableName.
When a class variable is declared as a public static final type, it is generally recommended that the class variable name be in uppercase letters. If static variables are not of public and final types, they are named in the same way as instance variables and local variables.
Description of class variables:
The static keyword in Java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than an instance of the class. The static can be: Variable (also known as a class variable) Method (also known as a class method) Block.
(translation)
The static keyword in Java is mainly used for memory management (that is, this will set aside memory space for sharing a certain amount between instances of the class). We can apply java's static keyword to variables, methods, statement blocks, and nested classes. Objects defined by static belong to the entire class, not an instance of a class.
Ordinary variables belong to a specific instance of a class, but variables modified with the static keyword will belong to a class. In other words, if we modify a general variable value through an instance of the class, only the value in this instance will be modified, and other instances will not be affected; but if we modify a class variable modified by the static keyword, the value of all instances of the class will be modified.
It's troublesome to say, you can take a look at a sample code:
Public class Static {public static void main (String [] args) {Example foo = new Example (); Example bar = new Example (); foo.staticVar = foo.normalVar = "foobar"; System.out.println (foo.staticVar + "\ t" + foo.normalVar); System.out.println (bar.staticVar + "\ t" + bar.normalVar);} class Example {static String staticVar = "Example"; String normalVar = "Example";}
Output:
Foobar foobarfoobar Example
As you can see, we only changed the value of the foo instance, but the change in the class variable staticVar modified by static also occurs in bar, while the normal member variable is not.
Using this feature, we can implement a class that can count the number of instances.
For example:
Public class CountingClass {public static void main (String [] args) {Counter a = new Counter (); Counter b = new Counter (); Counter c = new Counter (); System.out.println ("Total Counters:" + a.total + "=" + b.total + "=" + c.total);} class Counter {/ / self-counting class static int total = 0; public Counter () {total++; System.out.println ("+ total+" Counter is constructed. ") After reading this article, I believe you have a certain understanding of "what are the types of variables in Java". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 201
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.