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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:56:49+00:00 2026-05-17T06:56:49+00:00

I am confused in the following: In C++ we can pass a parameter to

  • 0

I am confused in the following:
In C++ we can pass a parameter to a function by reference (having declared it as a pointer or reference variable) and if we modify it inside the function, the changes are reflected to the caller when the function returns.
This is not happening in java and I am not sure I understand why.

E.g. this is a method from an object X

public boolean aMethod(int id, myClass aClass)
{
   //do some logic
   aClass = new MyClass();
   //configure argument object aClass
   return true;
}

In the calling code:

//some code processing
myClass obj = null;
if(X.aMethod(2,obj))
{
  obj.methodA();//start using the object
}

I use it in C++, i.e. to return a result that notifies that the function parameter can be used, but in java this does not work.
I enter the if(X.aMethod(2,obj)) branch but the obj is null. Why is it null?
Haven’t I assigned a memory address from the heap using new inside the method aMethod(int id, myClass aClass)? Am I not passing the “address” of obj in the function?
I was expecting to have the obj properly constructed and usable in the calling code. Have I misunderstood something concerning memory in java?

  • 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-05-17T06:56:49+00:00Added an answer on May 17, 2026 at 6:56 am

    Java passes everything by value – including references.

    What this means is that if you pass an object, you can modify properties of that object, and they will persist after you return, but you can’t replace the object in its entirety with a completely new object, because you can’t actually modify the reference – only what the reference points to.

    Before your aClass = line:

    Outside the function:
    
        obj ---> <foo>
    
    Inside the function:
    
        aClass ---> <foo>
    

    After your aClass = line:

    Inside the function:
    
        aClass ---> <bar>
    
    Outside the function:
    
        obj ---> <foo>
    

    The key thing to notice here is that aClass doesn’t point to obj – it points to <foo>. You’re not passing the address of obj, you’re passing the address of what obj points to. Thus, when you change what aClass points to, that doesn’t touch obj.


    As an alternative way of thinking about it:

    In Java,

    Bar foo = new Bar();
    

    is equivalent to C++,

    Bar *foo = new Bar();
    

    Thus when you pass foo to a function, you’re not passing the address of foo – you’re passing the address of the allocated object. Java doesn’t have the &-operator style pass-by-reference that C/C++ do.

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

Sidebar

Related Questions

I am confused with following string function echo strlen(l\n2); //give 3 in output where
I am confused from the result of the following code: I can't get my
I'm a little confused about how to pass an object to the pthread_create function.
I am very confused by the following warning about mass assignment: WARNING: Can't mass-assign
I am confused on the following: I can store a custom object for later
I am little bit confused about following problem & their solutions: i have 2
I am confused how the following works in MySQL. In the queries below, the
I'm confused by the following code: import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class
I'm confused by the following code: class MyException extends Exception {} class AnotherException extends
Perhaps it is a dumb question but I am confused with the following: Are

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.