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

How to master Java variables

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "how to master Java variables". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

01. Local variables

A variable declared in the body of a method is called a local variable, which can only be used within that method, and is not known to other methods in the class. Take a look at the following example:

/ * * @ author Wechat search for "Silent King II" and reply to the keywords PDF * / public class LocalVariable {public static void main (String [] args) {int a = 10; int b = 10; int c = a + b; System.out.println (c);}}

Where a, b, and c are local variables, which can only be used in the current main method.

Considerations when declaring local variables:

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 finished, they 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.

02, member variables

Variables inside the class but declared outside the method are called member variables, or instance variables. It is called an instance variable because it can only be accessed through an instance (object) of the class. Take a look at the following example:

/ * * @ author Wechat searches for "Silent King II" and replies with the keywords PDF * / public class InstanceVariable {int data = 88; public static void main (String [] args) {InstanceVariable iv = new InstanceVariable (); System.out.println (iv.data); / / 88}}

Where iv is a variable, it is a variable of reference type. The new keyword creates an instance of a class (also known as an object), assigns a value to the iv variable through the "=" operator, and the iv becomes a reference to the object, and the member variables can be accessed through iv.data.

Considerations when declaring member variables:

Member variables are declared in a class, but outside methods, constructors, and statement blocks.

When an object is instantiated, the value of each member variable is determined.

Member variables are created when the object is created and destroyed when the object is destroyed.

The value of a member variable should be referenced by at least one method, constructor, or statement block, so that the external can obtain instance variable information in these ways.

Member variables can be declared before or after use.

Access modifiers can modify member variables.

Member variables are visible to methods, constructors, or statement blocks in a class. In general, you should make the member variable private. Member variables can be made visible to subclasses by using access modifiers; member 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 time or in the constructor.

03. Static variable

Variables declared by the static keyword are called static variables (class variables), which can be accessed directly by the class, as shown in the following example:

/ * * @ author Wechat searches for "Silent King II" and replies with the keywords PDF * / public class StaticVariable {static int data = 99; public static void main (String [] args) {System.out.println (StaticVariable.data); / / 99}}

Where data is a static variable, through the class name. Static variables can be accessed without creating an instance of the class.

Considerations when declaring static variables:

Static variables are declared with the static keyword in the class, but must be outside the method constructor and statement blocks.

No matter how many objects a class creates, the class has only one copy of the static variable.

Static variables are rarely used except when declared as constants.

Static variables are stored in static storage.

Static variables are created at the beginning of the program and destroyed at the end of the program.

Has similar visibility to member variables. However, in order to be visible to the consumers of the class, most static variables are declared as public types.

The default values of static variables are similar to instance variables.

Static variables can also be initialized in static statement blocks.

04. Constant

In Java, the values of some data do not change. These data are called constants-member variables modified with the final keyword. The value of a constant cannot be changed once given!

Constants play two main roles in the process of running a program:

Represents a constant and is easy to modify (for example, the value of pi, final double PI = 3.14)

Enhance the readability of the program (for example, constants UP, DOWN are used to represent top and bottom, final int UP = 0)

Java requires that constant names must be capitalized. Take a look at the following example:

/ * * @ author Wechat search for "Silent King II" and reply to the keywords PDF * / public class FinalVariable {final String CHEN = "Shen"; static final String MO = "silent"; public static void main (String [] args) {FinalVariable fv = new FinalVariable (); System.out.println (fv.CHEN); System.out.println (MO);}}

"all right, third sister, that's all I have to say about the Java variable. Are you clear already?" After turning my stiff neck, I said to the third sister.

"Yes, second brother, I think I will see them again in the future, right?"

"that's a lot of times, just like you blink as many times a day."

This is the end of "how to master Java variables". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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