Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the concepts of Java variables

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the concepts of Java variables". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "what are the concepts of Java variables"?

Instance variable

The instance variable is also called Instance variables. It is not defined using the static keyword, and variables outside of any method, constructor, or block are instance variables. Instance variables are based on specific instances, and instance variables are not shared between instances, that is, each instance of an object has its own instance variable. Here is an example of an instance variable

Class Fruits {public String fruitName; / / fruitName; private int fruitNum; with public access / / fruitNum; with private access}

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;}}

1. How to identify instance variables

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

Instance variables can be modified with four access modifiers: public, protected, default, private

Instance variables can be modified with transient and final keywords

Instance variables cannot be modified with the keywords abstract, synchronized, strictfp, native, and static

Instance variables have default values, that is, instance variables can be used without initialization. Here are the initial values of commonly used instance variables

two。 The characteristics of instance variables

Above we understand the basic characteristics of instance variables and how to distinguish instance variables, let's talk about the characteristics of instance variables.

Instance variables can only be declared in the class, but outside the method, constructor, or any block.

When space is allocated for objects in the heap, an area is allocated for each instance variable.

Instance variables can only be used by creating objects. When the object is created using the new keyword, the instance variable is also created and destroyed when the object is reclaimed by the garbage collector.

Instance variables can be modified with access modifiers

The instance variable does not have to be forced to initialize, it has its own default value.

Each object has its own copy of instance variables, so modifying variables in one object does not affect instance variables in other objects.

Instance variables can only be used by creating object references.

2. Global variables

Global variables are also known as Global variables. If you have programming experience in other languages, such as C # and C++, you will be exposed to the concept of global variables. You can use the following code to create global variables

# include / / Global variable int A; int B; int Add () {return A + B;}

But in Java, there are no global variables. Because Java is an object-oriented programming language, all the content is part of the class. The reason Java does this is to prevent data and class members from being intentionally or unintentionally modified by other parts of other programs. So in Java, static variables are used for global access.

Third, static variables

Static variables are also known as Static variables. The definition of static variable is relatively simple. Static variable is a variable that belongs to this class, which is modified by the static keyword. Variables decorated by static are static variables, which can only be defined inside the class and outside the method.

Characteristics of static variables:

Static variables can only be modified with the static keyword, and they cannot be declared in methods, whether static or non-static.

Static variables are initialized before the program runs, and only once. Static variables have an initialization order, which we'll talk about later.

All instances of static variables share the same copy. That is, there is only one static variable, which is not copied as the object instance is created.

Static variables can be obtained by class name. The variable name is accessed and can be accessed without creating any objects.

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);}}

You can use static variables in non-static methods

IV. Class variables

Class variables are also known as Class variables in Java, class variables are static variables, and they are all modified with the static keyword, so if you hear about static variables again, it is also class variables.

5. Local variables

There is also a saying that there are only class variables, instance variables, and local variables in Java. There's no problem with this score. Someone might ask, where did you put the member variables and constants? Don't worry. We'll talk later.

Let's start with what is a local variable:

Local variables are also known as Local variables. It refers to variables defined in methods, constructors, or block code. The life cycle of local variables is destroyed with the completion of the execution of methods, constructors, and code blocks.

No matter how the concepts of some of the variables above are changed, the local variables are standing loose, sitting like a clock, walking like the wind, lying like a bow, calmly dealing with the comparison of different articles. What a convenient variable.

So we should take a closer look at the characteristics of such a good thing:

According to the definition, local variables are defined in methods, constructors, or code blocks.

Then the life cycle of the local variable is destroyed with the completion of the execution of the method, constructor, and code block.

Local variables cannot use access modifiers, such as the following code

Local variables are visible only in method declarations, constructors, or blocks, and local variables can only be used within the call to these methods, constructors, or blocks.

Local variables have no default values, so local variables should be initialized the first time they are used or declared

VI. Member variables

What? Are member variables instance variables in Java? Is this conclusion correct?

Constant

Verification process to verify this conclusion, we turned to the stackoverflow website.

Our search for java member variable and instance variable helped us locate the title.

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

In the first sentence, both instance variables and class variables are called member variables, and then the definition of variables in the JDK official website manual is given. It means that in Java, there are only three types of variables

Member variables defined in the class-called attributes

Variables defined in methods (including constructors) or block code-called local variables

Variables defined in the method definition-called parameters

Mm-hmm. no, no, no. Perhaps it is not very good to solve our problem, I once again turned to the JDK official website manual with the problem, and saw the definition of Variables.

It means that in Java, there are only the following types of variables (among other things, are you more reliable than the official website?)

Instance variable (non-static property): roughly means

Non-static properties are also called instance variables because their values are relative to each instance. In other words, the value of the instance variable is unique for each object.

Class variables (static properties): class variables are fields declared with the static modifier, which tells the compiler that no matter how many times the class is instantiated, there is only one copy of the variable. In addition, you can add the keyword final to represent constants.

Local variables: no special keyword declares a specified variable as a local variable, and the determination of its declaration depends entirely on the location of the declared variable.

Parameters: think about what is the most commonly used method? Of course it's the main method. How is the main method defined?

Public static void main (String [] args) {}

Whether the args is a variable of the array of String or not, we also call it a parameter, so there is no keyword to declare the parameter, and the identification of it as a parameter only depends on its declared position.

At this point, I believe you have a deeper understanding of "what are the concepts of Java variables?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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: 0

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report