0

Yep, purely theoretical question. It is not really clear to me what really is going on when we create a new object from the defined class.

For instance variables it seems reasonable to create new memory locations every time because they can have different values.

But I had a thought that it wouldn't make any sense in terms of memory to copy all class's methods to new object every time. So does java actually use class methods during the method call or does it create copies of the same methods in a new object?

user3081519
  • 2,659
  • 5
  • 25
  • 35
  • 3
    No. The methods are invoked through the object reference (if there is one), or the static reference (e.g. the class) if there isn't an instance. – Elliott Frisch Jan 10 '14 at 19:57
  • Pretty nice explanation: http://stackoverflow.com/questions/11993077/difference-between-static-methods-and-instance-methods – Kuba Spatny Jan 10 '14 at 20:01
  • Worth noting is that whether the method is public or not has no bearing on this. Whether it's static would (and obviously static methods are not copied). – Chris Hayes Jan 10 '14 at 20:07
  • @Kuba Spatny No, that is not what I was looking for. – user3081519 Jan 10 '14 at 20:11
  • @user3081519 It's not? As Andy Thomas pointed out in the comments of the accepted answer - it's not that simple. In the link is IMO much more details such as `On a deeper level, when the compiler puts an object together, it contains several pointers to methods. When those methods are executed it follows the pointers and executes the code at the far end.` – Kuba Spatny Jan 10 '14 at 20:16
  • @Kuba Spatny JVM is written in Java. http://stackoverflow.com/questions/8080617/why-doesnt-java-have-pointers – user3081519 Jan 10 '14 at 20:29
  • @user3081519 Umm yeah.. I don't really understand the last comment. But in the link are discussed explicit pointer as in C++. There are no pointer in Java in the high level that you could use in your code. However we are discussing deep level after compilation. – Kuba Spatny Jan 10 '14 at 20:35
  • @Kuba Spatny No. There are not. If you think differently - proof link, please. Or take a risk of asking this question on SO and we'll see. If you'll prove it I will admit myself wrong. – user3081519 Jan 10 '14 at 20:43
  • @KubaSpatny That answer was actually wrong! I've now edited it to correct it. – Robin Green Jan 11 '14 at 11:41
  • @RobinGreen very well done! Now it seems much clearer considering inheritance.. – Kuba Spatny Jan 11 '14 at 13:19

2 Answers2

5

A class method like so:

public class MyClass {
    public void myMethod(int arg) { }
}

can conceptually be considered the same as this at the JVM level:

public static void myMethod(MyClass this, int arg) { }

Where the this argument is passed implicitly based on the object you're calling the method on. From this definition, it is clear that you do not need a separate copy for each instance of the class.

As Andy Thomas mentions in the comments, there is more nuance to it than this at a low level; some information has to be retained at runtime for overridden methods, which don't exist for static methods.

Chris Hayes
  • 11,471
  • 4
  • 32
  • 47
  • It's not quite the same - consider virtual methods. – Andy Thomas Jan 10 '14 at 20:03
  • @AndyThomas True, but the concept applies without getting into a detailed discussion of vtable lookups. I feel that would largely bog things down. – Chris Hayes Jan 10 '14 at 20:05
  • @Chris Hayes Thanks, that was exactly what I was looking for. Basic textbooks don't go that deep into explanation. The second definition of the method really is an eyeopener. – user3081519 Jan 10 '14 at 20:15
3

It mostly (who knows what JVM's are out there) does not copy the methods per instance. It maintains the methods per class. It then resolves which class holds the method in question and calls that.

tilpner
  • 4,351
  • 2
  • 22
  • 45