Java - 我的意思是实例在内存中共享相同的实例方法吗?
例如:
public class Test {
private int a;
private int b;
public Test(int a , int b){
this.a = a;
this.b = b;
}
/* instance method - sum() */
public int sum(){
return a+b;
}
public static void man(String[] args) {
Test t1 = new Test(3,4);
Test t2 = new Test(5,6);
t1.sum();
t2.sum();
}
}
Run Code Online (Sandbox Code Playgroud)
我知道当将new关键字应用于类时,将复制类变量(属性).因此实例可以分别拥有自己的属性值.
但方法怎么样?它还会复制吗?如果是这样,只是浪费内存资源,因为方法总是相同的.
使用new关键字时JVM中出现了什么?非常感谢!
注意:官方文档参考是非常优选的:)
java ×1