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 8830713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:09:27+00:00 2026-06-14T08:09:27+00:00

I am confused with the memory allocation of references in Java when you pass

  • 0

I am confused with the memory allocation of references in Java when you pass them to the methods in Java. As a Test I did an example as below,

class Store{
public int[] buffer = new int[5];
 }

class ConsumeStore{
Store store;
public ConsumeStore(Store store){
    this.store = store; 
}

public int useStore(){
    return store.buffer[0];
}
  }

class ProduceStore{
Store store;
public ProduceStore(Store store){
    this.store = store;
}

public void putStore(){
    store.buffer[0] = 100;
}
 }

public class CheckRef {
  public static void main(String args[]){
  Store store = new Store();
  ConsumeStore consStore = new ConsumeStore(store);
  ProduceStore prodStore = new ProduceStore(store);
  prodStore.putStore();
  System.out.println(consStore.useStore());   
   }
 }

Well the output is 100.

Here I am passing the Store reference to ProducerStore. Inside ProducerStore I am assigning it to the class member of it. I am doing the same for ConsumerStore as well.

Could someone explain me how the memory allocation is done for Store referece and how it shared among the ProducerStore and ConsumerStore?

  • 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-14T08:09:28+00:00Added an answer on June 14, 2026 at 8:09 am

    Both your ProducerStore and ConsumerStore have a reference to the same object store, that you passed to each one.

    So when you modify store internal state (its buffer member), either externally or in any of the Producer/Consumer, all the classes that have reference to it are “updated” because they don’t hold a distinct value, they only hold a reference to the unique value store.

    Something different would happen if you reallocated the object entirely. For example:

    public void putStore(){
        store = new Store()
        store.buffer[0] = 100
    }
    

    In this case, the Consumer and the Producer would have a different store: Consumer would still have a reference to the original store, while Producer would have the new one defined within the Put method.

    You can think of it as: everytime you do a new, a new object is created in memory. Then when you pass it around to classes, everyone only has reference to that unique object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am always confused about static variables, and the way memory allocation happens for
I am somewhat confused about memory allocation of static variable in objective C. should
Possible Duplicate: Hiding instance variables of a class I have below class AbstractTest public
I am getting confused with memory allocation basics between Stack vs Heap . As
I am confused about the memory allocation in C++. Can anyone guide me as
I am confused about the memory allocation in C++ in terms of the memory
Update: I probably confused memory usage issues with the UI sharing same thread as
i m little confused on memory management in following scenario -(NSData *)getData{ NSData *TempData
I'm working on a memory pool implementation and I'm a little confused about pointers
I'm a little bit confused on how handle memory management when dealing with graphics

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.