Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7952005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:43:58+00:00 2026-06-04T02:43:58+00:00

I have the following block of code: class Student{ int age; //instance variable String

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:43:59+00:00Added an answer on June 4, 2026 at 2:43 am

    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
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have following code block public class Driver { static String x =
I have the following code block: - using System.ComponentModel.DataAnnotations; public class NewsItem { [RegularExpression(System.Configuration.ConfigurationSettings.AppSettings[UrlRegEx],
I have following code class Test { public: int &ref; int a; Test(int &x)
I have the following code block that fails: this.redisClient.hmset('user:' + userObj.getUserId(), { 'userId' :
I have the following java code: public class CheckInnerStatic { private static class Test
Hi All I have the following code: enter code here public class XMPPClient extends
I have the following code Snippet. class Program { static void Main(string[] args) {
I have the following sample piece of code, public class test implements Runnable{ public
I have controller and it has following code: class Company_ModuleName_NameController extends Mage_Core_Controller_Front_Action { public
Let's assume I have the following domain: public class Movie { public string Id

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.