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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:52:37+00:00 2026-05-31T19:52:37+00:00

I know that when it comes to passing objects in java, the reference of

  • 0

I know that when it comes to passing objects in java, the reference of object is passed as value. So what ever changes you make to that object is reflected in the original object as well. To avoid this we make use of immutability property in java.

So my question is, I have created an immutable class Employee, When I pass an object of this Employee class to some method in other class, I don’t want that method to make any changes to this object. Even if it does, I don’t want the changes to be affected to the original object created in the calling class.

Here is my sample code.

public final class Employee {
    int employeeId;

    public int getId() {
        return employeeId;
    }

    public void setId(int id) {
        this.employeeId = id;
    }

}

public class Worker {
    public static void process(Employee f, Employee g){
        int c = f.getId();
        f.setId(g.getId());
        g.setId(c);
    }
}

public class ImmutabilityTest{

    public static void main(String[] args) {
        Employee teamMember = new Employee();
        Employee teamLead = new Employee();
        teamMember.setId(5);
        teamLead.setId(10);
        System.out.println("Before process "+teamMember.getId() + " " + teamLead.getId());
        Worker.process(teamMember, teamLead);
        System.out.println("After process "+teamMember.getId() + " " + teamLead.getId());
    }
}

The output will be

Before process 5 10
After process 10 5

Is there any way to avoid the change in the values of the member variables of the Employee class ? (apart from making them final)If not then how are we actually exploiting the Immutability property in java in user defined class ?

Any suggestions/opinions/pointers are appreciated ! Thanks..

  • 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-31T19:52:38+00:00Added an answer on May 31, 2026 at 7:52 pm

    Immutability is not a property in Java that can be applied to an Object with a single keyword, its a strategy that needs to be implemented on a case by case basis. Generally speaking, you utilize the final keyword and cloning in order to accomplish this. Note that the deeper your object nesting, the greater the level of difficulty in achieving true object immutability.

    Here is a (more) immutable version of your Employee class:

    public class Employee {
        final int employeeId;
    
        public Employee(final int id) {
            super();
            this.employeeId = id;
        }
    
        public int getId() {
            return employeeId;
        }
    }
    

    Note that reflection in Java makes it practically impossible to achieve true immutability, even with the use of the final instance variables and cloning.

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

Sidebar

Related Questions

I always come across articles which claim that Java is interpreted. I know that
I have used Java, C++, .Net. (in that order). When asked about by-value vs.
I know that it's a subject that can raise a lot of debate, but
I know that one of the differences between classes and structs is that struct
I know that exceptions have a performance penalty, and that it's generally more efficient
We know that behind the scenes, the ASP.NET MVC framework will use reflection to
I know that I should put all the html elements in body tag, but
I know that one can define an 'expected' exception in JUnit, doing: @Test(expect=MyException.class) public
I know that it's possible to implicitly provide asynchronous interaction using: Asynchronous Delegates Asynchronous
I know that this has already been asked here but the answer (using a

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.