R
R
redasya2018-06-09 16:35:44
Java
redasya, 2018-06-09 16:35:44

Java keyword this, how does it work?

Hello!
Please clarify for a beginner
I know different situations in which the this keyword can be used in Java. But one definition is still not clear to me "a link to the current object", to which current one?
What if there are no objects created in the class?
And most importantly, what if there are several objects in the class?
Which one is "current"?
I read in one article that when creating a class, an object of the class is implicitly created, it is this that refers to it. But then the word this cannot replace an object when calling an object variable, by type object.variable is the same as this.variable, but how is this possible? After all, this points to a certain object that we did not create. In short, this definition is not at all clear "current object".
PS - Also, its use (this and super) for calling another constructor from one constructor is not entirely clear, it has never come in handy in my life, and I can’t even imagine a situation in which this can come in handy.
PSS - I re-read a bunch of articles, books, videos, and did not understand the essence of the "current object" because it is not clear what kind of object and what to do if there are no objects or there are several of them.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
G
GavriKos, 2018-06-09
@GavriKos

You are confused with terms. A class is just an "instruction", a "scheme". It's just that the class described in the code can't do anything. In order for it to do something, you need to create an object of the class. This is done explicitly via new.
Let's take a pen as an example. You have a handle class description. But this is not yet an object - just a description. By making New, you will create an instance of the handle. And there can be as many copies as you like, and with different parameters (color, size, etc.) - but all these are pens.
So. this in an INSTANCE points to THIS instance.

M
Maxim Moseychuk, 2018-06-10
@fshp

It looks like none of the commenters here have any idea how their code actually works.
You write code like this:

class Foo {
  void foo(String str) { ... };
}
...
Foo f = new Foo();
f.foo("abc");

The compiler will generate a bytecode that roughly corresponds to this code:
class Foo {
  static void foo(Foo this, String str) { ... };
}
...
Foo f = new Foo();
Foo.foo(f, "abc");

this is the implicit first parameter of any non-static class method. On which object you call the method, that one will be passed as the this parameter.
So basically this is not a regular keyword, it's just the name of a parameter in a method.

E
Elmo Sputterspark, 2018-06-09
@Sputterspark

Methods are always called on objects, not classes, unless they are static. Here on that object at which the method is called, this also refers.

D
Dmitry Alexandrov, 2018-06-09
@jamakasi666

If I have correctly tuned in to the TS wave, then I will chew it with accessible words.
To begin with, you need to have a correct understanding of what is happening in java, namely:
1) in java everything is an object, that's right, everything is all * . This means that absolutely anything is inherited from the Object class
2) there are (may be) differences in what exactly is contained in the class in the source code and the code when the program is executed and also after it is compiled. In simple terms, it can appear like this
- in the class code there is only the foo () method
- after compilation, it turns out that after some annotation-type events, the bar () method also appears in it. The important thing here is that it will appear * for all instances from this class
- when executed, it may turn out that the foo () method will be replaced by other code. This is already a reflection.
- when executed, it may turn out that SOME instances of the class will also have the mymethod () method inside. Those. not at all namely at some instances.
3) when executing a program * for absolutely everything, an instance is ALWAYS created explicitly or implicitly, even for a static class. Those. you may not have created an instance explicitly through new in the code, it was done by a runtime somewhere in the depths of Java!
4) a static class differs from a normal one only in that the static one always points only and only * to 1 single instance
5) super() , means that there will be a call to the PARENT method\constructor
6) super , WITHOUT BRACKETS, means jump to the PARENT variables. You can also call the PARENT method.
7) this always points to the current class instance. In other words, imagine that a class is a scope and when you write this you explicitly say that it refers to this instance and not to the class.
* Guru keep quiet that this is not entirely true in the special fundamental moments of
an instance - an instance of a class, i.e. Roughly speaking, it is created through new explicitly or implicitly
. I also recommend reading
this and examples here
Summarizing the above (with nuances):
- in java EVERYTHING and ALWAYS is an instance of some class
- based on the point above, there is ALWAYS at least one instance for any class
- based on the point above, there are often cases when it is necessary to access the parent's variable/method through super. Also, there is a need to refer to the method / variable of this particular instance or transfer this instance somewhere else without having a reference to this instance.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question