Using the keyword "this" in java

I was learning override method in Java when ai came across the this . After learning a lot about this on the Internet and other sources, I came to the conclusion that the this keyword is used when the name of the instance variables is the same for the constructor function parameters. Right or wrong?

+6
source share
10 answers

this is an alias or name for the current instance within the instance. It is useful for disambiguating instance variables from locales (including parameters), but it can be used on its own to simply reference member variables and methods, refer to other constructor overloads, or simply refer to an instance. Some examples of applicable use (not exhaustive):

 class Foo { private int bar; public Foo() { this(42); // invoke parameterized constructor } public Foo(int bar) { this.bar = bar; // disambiguate } public void frob() { this.baz(); // used "just because" } private void baz() { System.out.println("whatever"); } } 
+12
source
Keyword

this can be used for (it cannot be used with static methods):

  • Get a reference to the object through which this method is called inside it (instance method).
  • To avoid a field obscured by a method or constructor parameter.
  • A constructor call of the same class.
  • When overriding the method, this used to call the method of the current class.
  • Make a reference to the inner class. e.g. ClassName.this
  • To create an object of an inner class, for example enclosingObjectReference.new EnclosedClass
+11
source

You are right, but this is just a use case, not a definition. The this refers to the "current object". It is mainly used so that the object can pass itself as a parameter to the method of another object.

So, for example, if there is an object named Person and an object named PersonSaver , and you call Person.SaveYourself() , then Person can simply do the following: PersonSaver.Save( this ) ;

Now it’s just that this can also be used to disambiguate between instance data and constructor parameters or methods if they are identical.

+2
source

This keyword has the following uses: 1.used to refer to the current class instance variable

  class Student{ int id; String name; student(int id,String name){ this.id = id; this.name = name; } void display(){System.out.println(id+" "+name);} public static void main(String args[]){ Student s1 = new Student(111,"Karan"); Student s2 = new Student(222,"Aryan"); s1.display(); s2.display(); } } 

here the parameter and instance variable are the same, so we use this 2.used file to call the current constructor class

 class Student{ int id; String name; Student (){System.out.println("default constructor is invoked");} Student(int id,String name){ this ();//it is used to invoked current class constructor. this.id = id; this.name = name; } void display(){System.out.println(id+" "+name);} public static void main(String args[]){ Student e1 = new Student(111,"karan"); Student e2 = new Student(222,"Aryan"); e1.display(); e2.display(); } } 

3.this keyword can be used to call a method of the current class (implicitly)

4.This can be passed an argument in a method call

5.This can be passed an argument in a constructor call

6. It can also be used to return the current instance of the class.

+2
source

This refers to the current object . If you have a class with int A variables and an xyz method, then the part of the class has int A, just to distinguish which β€œA” you are referring to, you will use this.A. This is just one example.

 public class Test { int a; public void testMethod(int a) { this.a = a; //Here this.a is variable 'a' of this instance. parameter 'a' is parameter. } } 
+1
source

Usually the use of 'this' is reserved for instance variables and methods, not class methods ...

"class methods cannot use this keyword, since there is no instance for this applies to ..."

http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

Here is a trivial example ...

 public class Person { private String name; private int age; private double weight; private String height; private String gender; private String race; public void setName( String name ) { this.name = name; } public String getName() { return this.name; } public void setAge( int age) { this.age = age; } public int getAge(){ return this.age; } public void setWeight( double weight) { this.weight = weight; } public double getWeight() { return this.weight; } public void setHeight( String height ) { this.height = height; } public String getHeight() { return this.height; } public void setGender( String gender) { this.gender = gender; } public String getGender() { return this.gender; } public void setRace( String race) { this.race = race; } public String getRace() { return this.race; } public void displayPerson() { System.out.println( "This persons name is :" + this.getName() ); System.out.println( "This persons age is :" + this.getAge() ); System.out.println( "This persons weight is :" + this.getWeight() ); System.out.println( "This persons height is :" + this.getHeight() ); System.out.println( "This persons Gender is :" + this.getGender() ); System.out.println( "This persons race is :" + this.getRace() ); } } 

And for an instance of man ....

 public class PersonTest { public static void main( String... args ) { Person me = new Person(); me.setName( "My Name" ); me.setAge( 42 ); me.setWeight( 185.00 ); me.setHeight( "6'0" ); me.setGender( "Male" ); me.setRace( "Caucasian" ); me.displayPerson(); } } 
+1
source

if you have information about c, C ++, or pointers, in this language it is a pointer that points to the object itself. In java, everything is referenced. So this is a self reference in java. One of the needs of this keyword is as follows:

Think it's your class

 public class MyClass { public int myVar; public int myMethod(int myVar) { this.myVar = myVar; // fields is set by parameter } } 

If this keyword is not there, you will be embarrassed that this is a parameter or class field. When you use this .myVar, it refers to the field of this object.

0
source

In the event of a conflict of members and the local variable name, this keyword can be used to refer to a member variable, for example,

 public Loan(String type, double interest){ this.type = type; this.interest = interest; } 
0
source

I would like to change your language. The this keyword is used when you need to use a global class variable in constructors.

 public class demo{ String name; public void setName(String name){ this.name = name; //This should be first statement of method. } } 

this is a reference to the current object β€” the object whose method or constructor is being called. You can refer to any member of the current object from an instance or constructor method using this .

Another thing to keep in mind is that the this keyword may be the first expression of your method.

-1
source

This is used in java. We can use them in inheritance, and also use them when overloading a method and overriding a method. Since the actual name of the variable or instance name has the same name, we can use this complsary keyword. But sometimes it’s not as if we could not use this keyword ... For example: - class super {int x; super (int x) {this.x = x}}

-2
source

Source: https://habr.com/ru/post/904959/


All Articles