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 is the use of the this keyword in Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the use of the this keyword in Java, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

This keyword

The this keyword represents the caller object of the function to which it belongs.

What the this keyword does:

If there are member variables and local variables with the same name, the data of the local variable is accessed by default inside the method, and the data of the member variable can be specified through the this keyword.

In one constructor, you can call another constructor to initialize the object.

Note that the this keyword calls other constructors:

When the this keyword calls another constructor, the this keyword must be in the first statement in the constructor

The this keyword cannot call each other in the constructor because it is an endless loop.

Notes on the this keyword:

When there are member variables and local variables with the same name, the local variables are accessed inside the method (java is accessed by the "nearest principle" mechanism)

If a variable is accessed in a method and the value of the variable has a member variable, the java compiler adds the this keyword before the variable.

Note that the this keyword calls other constructors:

When the this keyword calls another constructor, the this keyword must be in the first statement in the constructor

The this keyword cannot call each other in the constructor because it is an endless loop.

This keyword example

Class Student {

Int id; / / ID card

String name; / / first name

/ / current situation: there are member variables and local variables with the same name, and local variables are used inside the method.

Public Student (int id, String name) {/ / the formal argument of a function is also a local variable.

/ / the constructor of a parameter of this class was called.

/ / this (name); / / if you can call the constructor of the following parameter here, you can solve the problem of duplicated code.

This (); / / calls the no-parameter constructor of this class.

This.id = id; / / this.id = id of the id; local variable assigns a value to the id of the member variable.

System.out.println ("the constructor of the two parameters called.")

}

Public Student () {

System.out.println ("No parameter constructor was called.")

}

Public Student (String name) {

This.name = name

/ / System.out.println ("the constructor of a parameter was called.")

}

}

Public class Demo1 {

Public static void main (String [] args) {

Student s = new Student (110, "Diga")

System.out.println ("serial number:" + s.id+ "name:" + s.name)

Student S2 = new Student ("W3CSCHOOL")

System.out.println ("name:" + s2.name)

}

}

Thank you for reading this article carefully. I hope the article "what is the use of this keywords in Java" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you 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