【发布时间】:2012-05-26 08:26:14
【问题描述】:
我有以下代码块:
class Student{
int age; //instance variable
String name; //instance variable
public Student()
{
this.age = 0;
name = "Anonymous";
}
public Student(int Age, String Name)
{
this. age = Age;
setName(Name);
}
public void setName(String Name)
{
this.name = Name;
}
}
public class Main{
public static void main(String[] args) {
Student s; //local variable
s = new Student(23,"Jonh");
int noStudents = 1; //local variable
}
}
我的问题与什么是局部变量、实例变量有关,以便知道它们在哪里分配,无论是在 HEAP 还是在 STACK 内存中。 在默认构造函数中,它似乎只存在一个局部变量,它是由 'this' 关键字创建的,但是如何' name = "Anonymous";'不认为是局部变量?它是对象类型,但那些也可以是本地变量,对吗? 顺便说一句,您能否举一个使用默认构造函数创建/实例化的对象的示例? 谢谢!
【问题讨论】:
-
JVM - Heap and Stack的可能重复
标签: java memory-management scope variable-types