-1

Reading "Thinking in Java" by Bruce Eckel at the moment. Reached "this" keyword point. It's not clear for me how really objects and "this" work. Bruce Eckel in his book says:

If you have two objects of the same type called a and b, you might wonder how it is that you can call a method peel( ) for both those objects:

//: initialization/BananaPeel.java
class Banana { void peel(int i) { /* ... */ } }  

public class BananaPeel {
    public static void main(String[] args) {
        Banana a = new Banana(),
        b = new Banana();
        a.peel(1);
        b.peel(2);
    }
} ///:~

If there’s only one method called peel( ), how can that method know whether it’s being called for the object a or b?

To allow you to write the code in a convenient object-oriented syntax in which you “send a message to an object,” the compiler does some undercover work for you. There’s a secret first argument passed to the method peel( ), and that argument is the reference to the object that’s being manipulated. So the two method calls become something like:

Banana.peel(a, 1);  
Banana.peel(b, 2); 

So, when we create an object it has it own methods copied from the class.

A obj = new A();
obj.callMethod();  //<-- object uses it's own method, not the class, right?

And according to the book the methods of a class are shared somehow between all the objects created from this class.

How does this mechanism work in the result?

I don’t get this part: If you have two objects of the same type called a and b, you might wonder how it is that you can call a method peel( ) for both those objects. If there’s only one method called peel( ), how can that method know whether it’s being called for the object a or b?

What does it mean “only one method called peel()

We have all the methods from the class created for each object. So we just call the method from the object.

What did Bruce Eckel mean?

Community
  • 1
  • 1
Coffemanz
  • 863
  • 2
  • 8
  • 17
  • @OleV.V. Yes, you are right. The title is incorrect for this question. Sorry. Corrected the title. – Coffemanz Feb 17 '19 at 09:21
  • You may edit the question and enter a title that covers better. Even better, you may also explain better what you don’t understand if you can. When you ask “How does it work?”, are you asking how to use it and what the result is, or are you asking how it’s implemented behind the scenes? (I was never fond of Bruce Eckel’s explanations. For my taste he goes too much behind the scenes, which sometimes creates more new questions than it answers.) – Ole V.V. Feb 17 '19 at 09:25
  • Nothing is copied from the class. The methods are in the class, and the object has a reference to the class. – user207421 Feb 17 '19 at 10:09
  • @OleV.V. Ok, tried to explain it a little bit more in the question. – Coffemanz Feb 17 '19 at 10:09
  • @user207421 Ok and what about variables. Does an object create a copy of a variable from the class. Let say I have String test = “test” in my class and create an instance of this class in which I change test = “test2”. And what about static methods then? If all the methods are stored in the class and objects just have references to the methods. Why I have to use static to call methods directly from the class? Thank you. – Coffemanz Feb 17 '19 at 10:16
  • 1
    No, an object is an *instance* of a class. The static variables are in the class, and the instance variables are in the instance: not in the class at all. All the methods are in the class, and the non-static methods require an instance to call them on, which is provided as a secret variable `this`, as Bruce Eckel states in your quotations. The static methods only require the class to call them on, because that's where they are, with no instance implications. – user207421 Feb 17 '19 at 10:20
  • @user207421 Ok, it makes sense to me now. Thanks! – Coffemanz Feb 17 '19 at 10:26
  • There are different ways to describe it. @user207421 When you say “The methods are in the class” you are talking implementation. Where I come from we say that each object (or instance) has a method by the name (and signature) that is defined in the class. It’s only a dispute about words, though. – Ole V.V. Feb 17 '19 at 11:50
  • For the sake of completeness: This question was previously closed as a duplicate of [When should I use “this” in a class?](https://stackoverflow.com/questions/2411270/when-should-i-use-this-in-a-class) After the edit it was clear to me that it isn’t a duplicate of that question (but possibly of some other original question that I haven’t found yet). So I have reopened. – Ole V.V. Feb 17 '19 at 11:52

1 Answers1

1

If you and I both laugh when something is funny, you may say that we share the ability to laugh.

I am thinking of sharing methods between instances of a class in much the same way. Bananas share the property that they can be peeled. The Banana class specifies what Banana objects are and what they can do. Some have described the class as a template for creating objects or a rubber stamp for stamping them. All bananas can be peeled, but the programmer still decides which ones s/he actually peels, and when. In programming terms: The Banana class specifies that every banana has a peel method, and what that method is.

When Eckel does a.peel(1); he’s specifying that he is peeling banana a (and not banana b just yet). As far as I am concerned, this all you as an object-oriented programmer need to know to use bananas and their methods.

Classes, objects and instance methods are described in many ways in many places. You may see if it helps you to search for other descriptions to supplement the one by Eckel.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    I get this idea. The question is: when I call a method, does an object use it's own method (which was described in the class) or it uses "shared" or "common" method from the class. I think user207421's answer covers this quite well. In particular now I got a better understanding of this: `Banana.peel(a, 1);` When we create an object and call any method, actually we are telling the class to call this method with the given object. Anyway, thank you very much. – Coffemanz Feb 17 '19 at 15:54
  • You can think of it either way. Of course duplicating the method code for every object would be a waste, so under the hood that doesn’t happen, but in my understanding that is an optimization trick, not something I as a programmer need to concern myself with. – Ole V.V. Feb 17 '19 at 16:01