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?