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 ways to use this,static,final,const in Java

2025-02-27 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 "what is the use of this,static,final,const in Java". 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!

1. This

When defining a variable with a class name, the definition should be just a reference, which can be used to access the properties and methods in the class. Is it enough and there should be a reference to access its own properties and methods? One nice thing that JAVA provides is the this object, which can refer to the properties and methods of the class in the class.

The Java keyword this can only be used inside a method. When an object is created, the Java virtual machine (JVM) assigns a pointer to the object that references itself, and the name of the pointer is this. Therefore, this can only be used in non-static methods in a class, and this must not appear in static methods and static blocks of code, which is clearly explained in the article "Java keyword static, final usage summary". And this is only associated with a specific object, not a class. Different objects of the same class have different this.

To sum up, there are three main uses of this:

1. Represents a reference to the current object!

2, represents the member variables of the class, not the function parameters, pay attention to the distinction between the function parameters and the member variables with the same name! In fact, this is a special case of the first usage, which is more commonly used, so it comes out to be stronger.

3. It is used to reference the constructor (that is, the constructor) that satisfies the specified parameter type in the constructor. But you have to be very careful here: only one constructor can be referenced and must be at the beginning!

Note: this cannot be used in the static method! So some people even define the static method as: there is no this method! Although exaggerated, it fully shows that this cannot be used in the static method!

II. Static

Static means "global" or "static" and is used to modify member variables and member methods. It can also form static static code blocks, but there is no concept of global variables in the Java language.

Member variables and member methods modified by static are independent of any object of this class. That is, it does not rely on class-specific instances and is shared by all instances of the class.

As long as this class is loaded, the Java virtual machine can find them based on the class name in the method area of the run-time data area. Therefore, the static object can be accessed before any of its objects are created, without having to reference any objects.

Static member variables and member methods modified with public are essentially global variables and global methods. When the object city of another class is declared, it does not generate a copy of the static variable, but all instances of the class share the same static variable.

The static variable can be preceded by a private modifier, indicating that the variable can be used in the static code block of the class, or in other static member methods of the class (of course, it can also be used in non-static member methods-nonsense), but it is important that it cannot be referenced directly by the class name in other classes. In fact, you need to understand that private is an access restriction, and static means that you can use it without instantiation, which makes it much easier to understand. The effect of adding other access keywords before static, and so on.

III. Final and const

The keyword final means "this is unchangeable" or "final".

1. Modified variable

The final member variable represents a constant and can only be assigned once, and the value does not change after the assignment.

two。 Modification method

Final methods cannot be overridden by subclass methods, but can be inherited.

3. Modifier class

The final class cannot be inherited, there are no subclasses, and all methods in the final class are final.

Const is a Java reserved keyword for later extension. Its usage is similar to final and is not commonly used.

This is the end of the content of "what are the ways to use this,static,final,const in Java". 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