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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:13:16+00:00 2026-06-09T23:13:16+00:00

I have following classes : Emp.java final public class Emp { private Integer id;

  • 0

I have following classes :

Emp.java

final public class Emp {

    private Integer id;
    private String name;
    private Department department;

    public Emp(Integer id, String name, Department department) {
        this.id = id;
        this.name = name;
        this.department = department;
    }

    public Department getDepartment() {
        return department;
    }

    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}

Department.java

public class Department {

    private Integer id;
    private String name;

    public Department(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }

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

    public void setName(String name) {
        this.name = name;
    }
}

EmployeeTest.java

public class EmployeeTest {

    public static void main(String args[]) {
        Department dept1 = new Department(1, "dept1");
        Emp emp = new Emp(1, "emp1", dept1);
        emp.getDepartment().setName("dept2");
        System.out.println("emp = "+emp);

    }
}

Here Emp class is not purely an immutable class because somehow I am able to change the values of Department (as shown in the example).

What are the best possible changes which will make Emp class a pure Immutable class ?

  • 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-09T23:13:18+00:00Added an answer on June 9, 2026 at 11:13 pm

    In getters for non-primitive field, use this structure

    public class Line {
    
       private final Point start;
       private final Point end;
    
       public Line(final Point start, final Point end) {
           this.start = new Point(start);
           this.end = new Point(end);
       }
    
       public Point getStart() {
           return new Point(start);
       }
    
       public Point getEnd() {
           return new Point(end);
       }
    }  
    

    So, simply create new instance of department that is equals to previous

    P.S. In my example you can see pure immutable class
    EDIT:
    Also you can add to Department class copy-contructor

    public Department(final Department dep)  
    { ... } 
    

    And to Employer

    getDepartment()  
    {  
       return new Department(department);  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following classes: class Department { private string departmentId; private string departmentName; private
I have the following classes: class Person { public string Name { get; set;
I have the following classes class Person { private String name; void getName(){...}} class
I have below this two classes .. class Emp //implements Comparable { String name,job;
I have the following relationship between two domain classes: class Emp { String name
I have the following classes: public class Group { private long id; private String
I have the following classes... public class Order { private Guid id; public Guid
I have 3 following classes: public class BaseProperty1{ public string Property1 {get; set;} }
i have the following classes: //Product class public class Product { @Id @GeneratedValue private
I have the following classes: public class Person { public String FirstName { set;

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.