The class goes by itself

I have a class that creates an object of type Smo. Then the object calls a static method from another class. The static method requires me to pass the object to it, which calls it. How to assign a caller as a passed parameter.

For example:

class Smo { Smo() { } void sponge() { car.dancing(??????); //////< ----------- how do I refer to self? } void dance() { //// do a little dance } } class Car() { Car() { } dancing(Smo smo) { smo.dance(); } } 
+6
java oop class
source share
5 answers

Use the this .

 car.dancing(this); 
+21
source share

use this keyword

Inside an instance or constructor method, this is a reference to the current object — the object whose method or constructor is called. You can access any member of the current object from within an instance method or constructor using this.

+7
source share

Use this to make the object refer to itself. Thus,

 car.dancing(this); 
+4
source share

yup: car.dancing (this);

+2
source share

was done (this): D

+2
source share

All Articles