-1

As per my understanding, in the OOP paradigm, the class is the blueprint of the objects that are created as per need. In context to the definition below from the Java docs which exactly is the current object that the this keyword is a reference to in the following snippet? The constructor that is being called is of the Point class. I also read this post which says that classes are not objects. What am I missing here?

public class Point {
public int x = 0;
public int y = 0;

//constructor
public Point(int x, int y) {
    this.x = x;
    this.y = y;
}}

Within an instance method or a constructor, 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 within an instance method or a constructor by using this.

learn_java
  • 31
  • 4
  • See also [the documentation](https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html). – Jason C Feb 26 '17 at 05:30
  • Perhaps http://stackoverflow.com/questions/1215881/the-difference-between-classes-objects-and-instances will clear up your terminology confusion. – Jason C Feb 26 '17 at 05:31
  • An instance of a class **is an object**. `this` instance of `this` class is having variables set – OneCricketeer Feb 26 '17 at 05:36
  • @JasonC I see that you are suggesting a terminology confusion, however, is it obvious from the content of the question? If so how? – learn_java Feb 26 '17 at 05:51
  • Perhaps http://stackoverflow.com/questions/1215881/the-difference-between-classes-objects-and-instances will shed some light. Great, rephrased. Move on. – Jason C Feb 26 '17 at 05:59
  • @JasonC Thanks, maybe I will move on from StackOverflow to somewhere else. If this is how beginners are left at the mercy of self-inferences I would rather be happy to move on. Remember, there does not exist a universal answer for every query that accommodates every individual. – learn_java Feb 26 '17 at 06:06
  • https://thenextweb.com/artificial-intelligence/2017/02/23/microsofts-new-ai-sucks-at-coding-as-much-as-the-typical-stack-overflow-user/ It is strange how people who are good at something can belittle the one's who just started their journey. – learn_java Feb 26 '17 at 06:13

1 Answers1

0

"This" usually tells you that,when you are using any variable that starts with "this" meaning you are using variable that belongs to this class. Not to any method or something.

In your case you have x as parameter and x as class variable . Both are having same name. When you say this.x , it refers you are calling class variable. and When you say x, it means you are using parameter of course inside that method. Same applicable for y also

One use,basically just to avoid confusion.

Praveen M
  • 443
  • 2
  • 11