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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:57:50+00:00 2026-05-27T06:57:50+00:00

if java is always pass variables by reference, why does eclipse generate the bean

  • 0

if java is always pass variables by reference, why does eclipse generate the bean with out any consideration.

instead of:
return myStr;

needs to be
return new String(myStr);

no?

Edit
Ok, my example was bad.
lets leave eclipse, When I want to return a Custom object. Do i need to create a “copy constructor” and return it, like that:

return new MyCustomObject(myCustomObject);


class MyCustomObject{

  private String str; 
  public MyCustomObject(String str){
    this.str = str;
  }

  public MyCustomObject(MyCustomObject obj){
    this.str =  obj.str;    
  }
}

Must I write that?

  • 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-27T06:57:50+00:00Added an answer on May 27, 2026 at 6:57 am

    No.

    In Java, every object variable is a reference. Objects cannot be passed by value, only primitives can (and always are). Well, that’s slightly misleading. The reference is passed by value, but you can think of everything being a reference, just not in a C++ sense.

    Perhaps it’s easiest to use an example.

    SomeObject foo;
    
    public void doSomething(SomeObject bar) {
        bar.whatever();
        bar = new SomeObject();
        bar.someOtherMethod();
    }
    
    public void doStuff() {
        foo = new SomeObject();
        doSomething(foo);
    }
    

    So, foo is a reference to an instance of SomeObject. When doSomething is called, the value of that reference is copied to bar, so now foo and bar are references to the same SomeObject.

    The line bar.whatever() calls whatever on the same object that foo refers to.

    bar = new SomeObject() means that foo and bar now refer to different SomeObject instances, so someOtherMethod is not called on the object that foo refers to.

    This is completely different to C++, where

    void doSomething(SomeObject& bar) {
        bar = whatever;
    }
    

    has a totally different meaning. You really should not ever think of Java in C++ terms.

    Regarding your example, Strings are immutable in Java so it wouldn’t matter even if objects could be passed by value.

    Regarding your second example, if you want to return an object that the caller cannot use to pollute your internal state then, yes, you need to have a copy constructor (or something equivalent).

    For example:

    class ClassWithInternalList {
        private List<String> reallyImportantData;
    
        public List<String> getImmutableViewOfData() {
            // Take advantage of the java.util.Collections tool for making a List immutable.
            return Collections.unmodifiableList(reallyImportantData);
        }
    
        public List<String> getSafeCopyOfData() {
            // Return a copy that the caller can modify without changing reallyImportantData.
            return new ArrayList<String>(reallyImportantData);
        }
    
        public List<String> justGetTheData() {
            // Return a reference to reallyImportantData that the caller can modify at will.
            return reallyImportantData;
        }
    }
    

    You can choose the appropriate type of return value (normal reference, immutable view or copy) depending on the situation. Any or all of the three options could be appropriate depending on exactly what you are doing.

    java.util.Collections makes it easy to get an immutable view of a Collection, but for custom classes you’ll need to do your own immutable-ness.

    Remember that you only need to do this if there is an issue with mutability. Your MyCustomObject example is still immutable (since the caller cannot change any state in the returned MyCustomObject instance), so the question is still kinda moot.

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

Sidebar

Related Questions

In Java, we can always use an array to store object reference. Then we
as I know, Java always invoke a method by pass-by-value. but I see the
According to: http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html 4.5.2 Variables of Reference Type A reference type can hold a
Possible Duplicate: Is Java pass by reference? Hi guys, I have a question about
Every time I need to work with date and/or timstamps in Java I always
One meme that gets stressed with Java development is always use ArrayList over Vector.
I always hear that Java being open-source is a big benefit, but I fail
Always when I run java application it will display in Windows Task Manager is
I always come across articles which claim that Java is interpreted. I know that
I have always liked the documentation on Java APIs, generally speaking, but I know

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.