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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:25:35+00:00 2026-06-09T11:25:35+00:00

If I have a class like the one below: public class Foo() { private

  • 0

If I have a class like the one below:

public class Foo()
{
    private RandomObject randomObject = new RandomObject();

    public RandomObject GetRandromObject()
    {
        return randomObject;
    }
}

And in another class I do this:

public class Goo()
{
    private Foo fooObject = new Foo();

    public Goo()
    {
        RandomObject ro = fooObject.GetRandomObject();

        ro.ChangeNumberVariable(23);
    }
}

Will the fooObject have the randomObject NumberVariable changed to 23?

If not would I just have to have a method in Foo called SetRandomObject and just pass in ro? Would this be a good substitute for passing by reference in Java?

What if I just did this:

public class Goo()
{
    private Foo fooObject = new Foo();

    public Goo()
    {
        fooObject.GetRandomObject().ChangeNumberVarialbe(23);
    }
}

Is it still not changing the NumberVariable?

  • 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-09T11:25:39+00:00Added an answer on June 9, 2026 at 11:25 am

    In both cases fooObject.randomObject would have NumberVariable changed to 23. They are pretty much equivalent just the former uses an extra reference.

    This does not make Java pass-by-reference. Java is pass by value. Any time you pass something to a method as a parameter it is copied, even if what you pass is a reference to an object.

    Though you can use that copied reference to access and mutate the object on the end of it, as you are doing here, any re-assignment of that reference cannot escape the method.

    In your first example doing:

    ro = new RandomObject();
    

    would not change anything about fooObject.randomObject.

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

Sidebar

Related Questions

I have a class like the one below: class Foo { private: std::map<std::string, Bar*>
I have one of my class method like this shown below : public function
Say I have a class with three constructors, like the one below: public class
I have a class that contains a boolean field like this one: public class
I have a worker class like the one below: class Worker{ public: int Do(){
I have 2 class like below, public class CheckInBean implements Serializable{ private String user;
I have a Userdetails class like the one below public class UserDetails { public
I have a simple model like this one: class Artist(models.Model): surname = models.CharField(max_length=200) name
Let's suppose I have xml like this one: <Server Active=No> <Url>http://some.url</Url> </Server> C# class
I have a model like the following one: public class TestModel{ public IList<Field> Fields

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.