Local Variables, Object references and it's memory allocation
Asked Answered
H

2

5

I have the following block of code:

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
 }
}

My question is related to what are local variables, instance variables in order to know where they are allocated, wether in HEAP or STACK memory. In the default constructor it seems to exist only one Local variable, which the one created by the 'this' keyword, but howcome ' name = "Anonymous";' is not considered to be a local variable? It's pf the Object type, but those can be local varibles too, correct? By the way can you give an example of an object created/instantiatedwith the default constructor? Thank you!

Hurling answered 18/5, 2012 at 18:45 Comment(1)
possible duplicate of JVM - Heap and StackLilalilac
B
9

In short, names of any kind only store references, they do not store objects directly.

A name that is completely constrained to a block of code has allocated storage for the reference, on the stack frame, which is on a Stack that is private to the Thread.

A name that is a member of a class has allocated storage for the reference in the heap, within the Object that represents the instance of that class.

A name that is a static member of a class has allocated storage for the reference in the heap, within the Object that represents the instance of the Class Object of that class.

All objects live on the heap; however, references to them may live within other objects on the heap, or within reference placeholders on the stack.

All primitive data types are stored where the reference would have been stored.

class Student {

  int age;         // value stored on heap within instance of Student
  String name;     // reference stored on heap within instance of Student

  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;                    // reference reserved on stack
        s = new Student(23, "John");  // reference stored on stack, object allocated on heap
        int noStudents = 1;           // value stored on stack
  }
}
Binate answered 18/5, 2012 at 18:54 Comment(2)
I think that this might be a really stupid question\doubt but when you say "A name that is a member of a class has allocated storage for the reference in the heap" that is the cas of name = "Anonymous"; ? And is the following the case of a reference placeholder that live is the stack? s = new Student(23, "John");Hurling
Not a stupid question. An "Anonymous" class is a class, so it is allocated on the heap; however, the heap holds information that cannot be tied back to a particular type, as it was specified from an abstract type and code to fill in the abstract sections. Sometimes the anonymous class is bound to a name, other times the class is just used directly without an intermediate name; however, the class is never given its own unique base type.Binate
E
-1

All primitives var (int, double, boolean) are created on the stack. The more complexed objects that allocated with "new" are in the heap and your variable is simply a reference to it (pointer).

a local variable is a var that exists in a specific scope like "Student s" exists only in Main method (it can be either in the stack or in the heap)

Earwig answered 18/5, 2012 at 18:51 Comment(1)
No, this isn't quite true. Check the link to the duplicate.Lilalilac

© 2022 - 2024 — McMap. All rights reserved.