-2

Can someone please explain the difference between this and getClass in Java? Aren't they the same? getClass returns the class of an object and this does the same thing, right?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Jowan
  • 11
  • 3
    `this` returns the object itself from inside an instance method. – Sergey Kalinichenko Nov 05 '21 at 19:55
  • 3
    Imagine you have a class called Horse and create an Object like `new Horse("Mr. Ed");`. Calling `getClass()` in that object will tell you its a Horse (return Horse.class) while `this` will refer to the specific "Mr. Ed" horse object. – OH GOD SPIDERS Nov 05 '21 at 19:58
  • 1
    What makes you think / say they even remotely produce the same thing?? – luk2302 Nov 05 '21 at 20:04
  • 1
    Does this answer your question? [What is the meaning of "this" in Java?](https://stackoverflow.com/questions/3728062/what-is-the-meaning-of-this-in-java) – OldProgrammer Nov 05 '21 at 20:06
  • 2
    Before anything you should probably familiarize yourself with [difference between a variable, object, and reference](https://stackoverflow.com/q/32010172) – Pshemo Nov 05 '21 at 20:18
  • "Aren't they the same" you can easily check this by checking `(Object) this == getClass()` (you need the cast to get it to compile) and `this.equals(getClass())`. – Andy Turner Nov 06 '21 at 12:49

1 Answers1

3

The difference between an object and a class

No, they are pretty different, so much that it doesn’t make much sense to compare them.

  • this can be regarded as a constant reference to the containing object. So not a class, and not being a method it doesn’t return anything. It just is a reference.
  • getClass() is a method and returns a reference to an object’s class. So not the object itself. Also being a method you can trivially call getClass() on a different object — any object, in fact — whereas this is always the containing object.

An example may demonstrate it. The following isn’t meant to be meaningful as a program that anyone could use for an external purpose.

package ovv.so.objects.basic;

public class ThisAndGetClassDemo {

    int i;
    
    public void foo() {
        i = 3;
        System.out.println(this);
        System.out.println(getClass());
        System.out.println(this.getClass());
        System.out.println("A string".getClass());
    }
    
    @Override
    public String toString() {
        return "ThisAndGetClassDemo " + i;
    }
    
    public static void main(String[] args) {
        new ThisAndGetClassDemo().foo();
    }

}

Output from the program is:

ThisAndGetClassDemo 3
class ovv.so.objects.basic.ThisAndGetClassDemo
class ovv.so.objects.basic.ThisAndGetClassDemo
class java.lang.String

So you see:

  • this gives us the object (printing it implicitly calls toString() and then prints the return value).
  • getClass() and this.getClass() produce the same output. Calling getClass() without qualification (without mentioning which object’s getClass method we want to call) calls the object’s own getClass method — exactly the same as what this.getClass() does. In both cases the class is printed, not the object itself.
  • Finally "A string".getClass() gives us the String class because "A string" is an instance (an object) of that class.

I do understand your confusion, though. Every now and then we hear programmers being sloppy and in what they say not making that distinction between an object and a class — in particular in situations where there is only one object of a given class in play (which happens often). It’s like saying “horse” without making explicit whether I mean the horse (the object; given there is only one around) or I mean the concept of a horse (the class).

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    interesting ones to add: `ThisAndGetClassDemo.class` and `getClass().getClass()` :-). – bvdb Nov 05 '21 at 21:02
  • I agree,@bvdb. Considering the OP is probably a beginner, I think I have broadened the question enough already, so I will leave those as an exercise to the interested reader (who should have an IDE and hence an opportunity to try them out). – Ole V.V. Nov 05 '21 at 21:29
  • many thanks for the detailed explanation – Jowan Nov 06 '21 at 19:25