5

My colleague just asked me a really interesting question and I cannot give him an answer.

Let's assume that we have got the following class:

public class Person {
    String name;

    public Person(String name) {
        this.name = name;
    }

    public void print() {
        System.out.println("xxx");
    }
}

Now, we are creating the objects:

Person p1 = new Person("a");
Person p2 = new Person("b");
Person p3 = new Person("c");
Person p4 = new Person("d");
Person p5 = new Person("e");
Person p6 = new Person("f");
Person p7 = new Person("g");
Person p8 = new Person("h");

The question was:

Do we keep information about the available methods in each single object? If we create a new object p9, will the JVM create the object with information about only fields or will it also add to this object information about methods?

Another question:

What happens if I invoke p1.print()? Does p1 have to ask the Person class to provide this method, or is it already saved in p1 object?

vallentin
  • 23,478
  • 6
  • 59
  • 81
ruhungry
  • 4,506
  • 20
  • 54
  • 98
  • 4
    @Vallentin What terms would you suggest he google? – Kevin Workman Feb 04 '14 at 14:22
  • @KevinWorkman How about the question? – vallentin Feb 04 '14 at 14:23
  • 2
    @Vallentin I actually find that question very interesting. Remember, that Google very often finds StackOverflow questions as the answer. The question just needs to be asked. However, in that case there are similar questions on SO, but the point is that it is QA site. – Eel Lee Feb 04 '14 at 14:24

4 Answers4

5

The code for methods isn't duplicated for all the instances, that would be completely unnecessary. The code lives in a special area in the memory, and it's shared by all the instances. The memory required by instance variables on the other hand naturally is owned by every instance.

As to how a method is called, the object doesn't really need to ask the class every time it calls a method, it has a pointer to the method's code and can just call it right away.

For more information on the inner workings of the JVM, refer here: http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html

Kayaman
  • 72,141
  • 5
  • 83
  • 121
2

In general, code is represented once, no matter how many objects there are. The OO concept of objects "having" methods is just an abstraction. So, in essence, regardless of whether you method is instanced or static, behind the scenes it still "belongs" to the class.

For final methods, the call uses static binding (it's already predetermined which method will be called, based on the type of the expression).

For virtual methods, something more interesting happens. Depending on the implementation, either the object contains the address to the correct method (not the method itself, again) or the runtime reflects on that object to determine its actual class and find the appropriate method in the hierarchy.

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
2

I don't know how the Oracle JVM does it, but in general object-oriented programming systems, an object has a hidden instance variable that points to its class. All of the instances point to the same class object, and the pointers to the instance methods are part of the class.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
1

JVM does a wonderful job by not binding object and methods together. To understand it's functionality clearly you need to understand it's underlaying architecture.

JVM has a class loader system which consist of below resources:

  1. Method area
  2. Heap
  3. Java Stack
  4. PC register
  5. Native method stack.

When we create an object the class gets loaded(lazy load) and it gets all the required resources. Here in method area all the methods reside and objects reside in heap and which are shared among all the threads. JVM allocates/organizes all the memory needed to execute a program into several runtime memory areas.

Helios
  • 851
  • 2
  • 7
  • 22