see märksõna sisse Java

⚡ Nutikas kokkuvõte

Java this Keyword is a reference variable that points to the current object inside a method or constructor. Lessons cover purpose, common uses, naming conflicts, constructor chaining, and a worked example.

  • 🔑 Määratlus: this references the current instance of a class.
  • 🧱 Lahenda konfliktid: Use this.field to distinguish instance variables from parameters with identical names.
  • 🪜 Constructor Use: this() invokes another constructor of the same class for cleaner initialization.
  • Pass and Return: Send this as an argument or return it to support method chaining.
  • 🧪 Töötatud näide: The Account class shows how this fixes zero output caused by parameter shadowing.
  • 🤖 AI abi: AI coding tools detect missing this prefixes and suggest accurate refactors.

see märksõna sisse Java

Milles see märksõna on? Java?

. see märksõna sisse Java on viitemuutuja, mis viitab meetodi või konstruktori praegusele objektile. Selle märksõna kasutamise peamine eesmärk Java is to remove the confusion between class attributes and parameters that have the same name.

Selle märksõna kasutamine Java

Following are various uses of the this keyword in Java:

  • It can be used to refer to the instance variable of the current class.
  • It can be used to invoke or initiate the current class constructor.
  • It can be passed as an argument in a method call.
  • It can be passed as an argument in a constructor call.
  • It can be used to return the current class instance.

Click siin kui video pole juurdepääsetav

How to Understand the this Keyword With an Example

see märksõna sisse Java
Java see märksõna Näide

  1. klass: klassi konto
  2. Eksemplari muutuja: a ja b
  3. Meetod Määra andmed: a ja b väärtuse määramiseks.
  4. Meetod Näita andmeid: a ja b väärtuste kuvamiseks.
  5. Peamine meetod: where we create an object for the Account class and call methods set data and show data.

Walking Through the Compiler Confusion

Let us compile and run the code.

Meie eeldatav väljund A ja B jaoks tuleks lähtestada vastavalt väärtustele 2 ja 3.

But the value is 0. Why? Let us investigate.

see märksõna sisse Java

In the method Set data, the arguments are declared as a and b, while the instance variables are also named a and b.

see märksõna sisse Java

During execution, the compiler is confused. Whether “a” on the left side of the assigned operator is the instance variable or the local variable. Hence, it does not set the value of “a” when the method set data is called.

see märksõna sisse Java

The solution is the this keyword.

Append both “a” and “b” with the Java sellele märksõnale järgneb punkt (.) operaator.

see märksõna sisse Java

During code execution when an object calls the method setdata, the keyword this is replaced by the object handler “obj.” (See the image below.)

see märksõna sisse Java

Nüüd teab koostaja,

  • The “a” on the left-hand side is an instance variable.
  • Whereas the “a” on the right-hand side is a local variable.

. muutujad on õigesti lähtestatud ja kuvatakse oodatav väljund.

see märksõna sisse Java

How the Compiler Handles Multiple Objects

Suppose you choose different names for your instance variable and method arguments.

see märksõna sisse Java

Kuid seekord loote kaks klassi objekti, millest igaüks kutsub välja andmemäärangu meetodi.

How will the compiler determine whether it is supposed to work on the instance variable of object 1 or object 2?

see märksõna sisse Java

Noh, see on kompilaator lisab kaudselt the instance variable with the this keyword (image below).

see märksõna sisse Java

Nii, et kui objekt 1 kutsub välja andmekomplekti meetodi, lisatakse eksemplari muutuja selle võrdlusmuutuja juurde.

see märksõna sisse Java

Sel ajal, kui objekt 2 kutsub välja andmekomplekti meetodi, muudetakse objekti 2 eksemplari muutujat.

see märksõna sisse Java

The compiler handles this automatically. You do not have to append the this keyword explicitly unless an exceptional situation requires it.

Example: this Keyword in Java koos Code

Näide: To learn the use of the this keyword.

Step 1) Kopeerige järgmine kood märkmikusse.

class Account{
int a;
int b;

 public void setData(int a ,int b){
  a = a;
  b = b;
 }
 public void showData(){
   System.out.println("Value of A ="+a);
   System.out.println("Value of B ="+b);
 }
 public static void main(String args[]){
   Account obj = new Account();
   obj.setData(2,3);
   obj.showData();
 }
}

Step 2) Salvesta, kompileeri ja käivita kood.

Step 3) Value of a and b is shown as zero? To correct the error, append line # 6 and 7 with the see märksõna.

this.a =a;
this.b =b;

Step 4) Save, compile, and run the code. This time around, the values of a and b are set to 2 and 3 respectively.

KKK

The this keyword refers to the current object whose method or constructor is executing. It lets the code access that object’s own instance fields and methods.

No. The this keyword belongs to an instance, while static methods exist at the class level. Using this inside a static method causes a compile-time error.

this() invokes another constructor of the same class as the first statement. It avoids duplicate code and lets one constructor delegate setup to an overloaded one.

AI coding assistants flag shadowed parameters, add the missing this prefix, and rewrite constructors to use this() chaining and fluent setters that return this.

AI tutors explain object identity, show instance versus local scope, and produce step-by-step examples with quizzes reinforcing when to use the this keyword.

Vaadake meie artiklit Java Intervjuu küsimused: - Kliki siia

Võta see postitus kokku järgmiselt: