HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

An immutable person

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
immutablepersonstackoverflow

Problem

I am trying to model person in OOPS using immutability.

I have created an object bond with age as 25. To change the age I have created a new object and returned it.

Is there any better way to change the age instead of passing all the values of object again? Is this a correct way to do?

import java.io.*;
import java.util.*;

class Solution
{
    public static void main(String args[])
    {
        System.out.println("TestMessage");      
        Person bond = new Person("bond",25,"100 High","Park St","10000","NewYork");
        System.out.println("The age of bond is "+bond.getAge());     
        //If I want to change age of John,create new object as the preson object is immutable.
        bond = bond.changeAge(50);
        System.out.println("New age of bond is "+bond.getAge());      

    }
}

//Immutable class....
//Rule 1:No setter..getters can be present
//Rule 2:Ensure that the class can’t be extended
//Rule 3:Make all fields final, To avoid problems in multithreaded env.
//Rule 4:Make all fields private
//Rule 5:Ensure exclusive access to any mutable components
final class Person
{
  private final String name;
  private final int age;
  private final String streetAddr1;
  private final String streetAddr2;
  private final String pinCode;
  private final String city;

  private int salary;
  private float bonusPercentage;

  public Person(String name,int age,String streetAddr1,String streetAddr2,String pinCode,String city)
  {
    this.name=name;
    this.age=age;
    this.streetAddr1 = streetAddr1;
    this.streetAddr2 = streetAddr2;
    this.pinCode = pinCode;
    this.city = city;
  }  
  public int getAge()
  {
    return this.age;
  }  

  public Person changeAge(int age)
  {
    return new Person(this.name,age,this.streetAddr1,this.streetAddr2,this.pinCode,this.city);
  }
}

Solution

General Review

Bad Practices

First, you import the whole package:

import java.io.*;
import java.util.*;


It is usually better practice to import specific classes. Also, I don't even see the use of any of these packages. Don't import what you don't need: it will affect performance.

Formatting

Your second class seems to have two-space indents. Java standard conventions suggests four-space indents.

Your spacing is also inconsistent. Consider:

this.name=name;
this.age=age;
this.streetAddr1 = streetAddr1;
this.streetAddr2 = streetAddr2;
this.pinCode = pinCode;
this.city = city;


You have some lines with spaces, some lines without. Choose a style, and stick with it. I suggest the spaces version, because it is easier to read...

Spacing here:

public Person(String name,int age,String streetAddr1,String streetAddr2,String pinCode,String city)


and here:

Person bond = new Person("bond",25,"100 High","Park St","10000","NewYork");


Put spaces after the commas; this will make it easier to read.

Naming

Usually, setters' method names begin with set. It is also suggested that it is so. changeAge should be setAge.

Immutable Class Suggestions


Is this a correct way to do?

Well, read on!

Immutable classes should not have any set methods. This is because an immutable class should only represent objects that don't change. If a person needs to be changed (not likely though), then the most obvious way to do so is to directly call the Constructor for a new Object, as a person with a different characteristic would not be the same person.

Code Snippets

import java.io.*;
import java.util.*;
this.name=name;
this.age=age;
this.streetAddr1 = streetAddr1;
this.streetAddr2 = streetAddr2;
this.pinCode = pinCode;
this.city = city;
public Person(String name,int age,String streetAddr1,String streetAddr2,String pinCode,String city)
Person bond = new Person("bond",25,"100 High","Park St","10000","NewYork");

Context

StackExchange Code Review Q#107575, answer score: 7

Revisions (0)

No revisions yet.