Skip to main content
deleted 52 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Immutable objects in javaJava

I just finished studying immutable objects and their advantages so I thought I'd create one of my own. Here is my employee class that extends a person class. It is not mutable since iI have getters to my mutable date objects.

package objects.objects;

import java.util.Date;

public class Person {

    private final String name;
    private final Date dateOfBirth;
    
    Person(String name,Date dateOfBirth){
        this.name=name;
        this.dateOfBirth=dateOfBirth;
        
    }
    public String getName() {
        return name;
    }
    public Date getDateOfBirth() {
        return dateOfBirth;
    }
 
}

EmployeeEmployee Class:

Don't share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods

Furthermore, the Oracle doc says:

Don't allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is to make the constructor private and construct instances in factory methods.

What if I want my class to be subclassed? What harm is there if I don't make my class final or have a private constructor?

Also, could you point out apart from the date being mutable object references, is? Is there any other flaw in my code that violates the mutable policy or any other improvements as such?

Edit: furthermore The oracle doc says : "Don't allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is to make the constructor private and construct instances in factory methods"... What if i want my class to be subclassed? what harm is there if i dont make my class final or have a private constructor? I am unable to follow this line??

Immutable objects in java

I just finished studying immutable objects and their advantages so I thought I'd create one of my own. Here is my employee class that extends a person class. It is not mutable since i have getters to my mutable date objects.

package objects.objects;

import java.util.Date;

public class Person {

    private final String name;
    private final Date dateOfBirth;
    
    Person(String name,Date dateOfBirth){
        this.name=name;
        this.dateOfBirth=dateOfBirth;
        
    }
    public String getName() {
        return name;
    }
    public Date getDateOfBirth() {
        return dateOfBirth;
    }
 
}

Employee Class:

Don't share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods

Also could you point out apart from the date being mutable object references, is there any other flaw in my code that violates the mutable policy or any other improvements as such?

Edit: furthermore The oracle doc says : "Don't allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is to make the constructor private and construct instances in factory methods"... What if i want my class to be subclassed? what harm is there if i dont make my class final or have a private constructor? I am unable to follow this line??

Immutable objects in Java

I just finished studying immutable objects and their advantages so I thought I'd create one of my own. Here is my employee class that extends a person class. It is not mutable since I have getters to my mutable date objects.

package objects.objects;

import java.util.Date;

public class Person {

    private final String name;
    private final Date dateOfBirth;
    
    Person(String name,Date dateOfBirth){
        this.name=name;
        this.dateOfBirth=dateOfBirth;
        
    }
    public String getName() {
        return name;
    }
    public Date getDateOfBirth() {
        return dateOfBirth;
    }
}

Employee Class:

Don't share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods

Furthermore, the Oracle doc says:

Don't allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is to make the constructor private and construct instances in factory methods.

What if I want my class to be subclassed? What harm is there if I don't make my class final or have a private constructor?

Also, could you point out apart from the date being mutable object references? Is there any other flaw in my code that violates the mutable policy or any other improvements as such?

Updated question
Source Link
Ishan Soni
  • 808
  • 2
  • 11
  • 17

Edit: furthermore The oracle doc says : "Don't allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is to make the constructor private and construct instances in factory methods"... What if i want my class to be subclassed? what harm is there if i dont make my class final or have a private constructor? I am unable to follow this line??

Edit: furthermore The oracle doc says : "Don't allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is to make the constructor private and construct instances in factory methods"... What if i want my class to be subclassed? what harm is there if i dont make my class final or have a private constructor? I am unable to follow this line??

Tweeted twitter.com/#!/StackCodeReview/status/522011218056204289
formatting
Source Link
rolfl
  • 97.5k
  • 17
  • 218
  • 418

I just finished studying immutable objects and their advantages so iI thought i'dI'd create one of my own. Here is my employee class that extends a person class. It is not mutable since i have getters to my mutable date objects.

package objects.objects;

import java.util.Date;

public class Person { 

    private final String name;
    private final Date dateOfBirth;
    
    Person(String name,Date dateOfBirth){
        this.name=name;
        this.dateOfBirth=dateOfBirth;
        
    }
    public String getName() {
        return name;
    }
    public Date getDateOfBirth() {
        return dateOfBirth;
    }

}
package objects.objects;

import java.util.Date;

public final class Employee extends Person{

    private final String empolyeeID;
    private final Designation designation;
    private final double salary;
    private final Date dateOfJoining;
    
    
    public Employee(String Name,Date dateOfBirth,String employeeID,Designation designation,double salary,Date dateOfJoining)
    {
        super(Name,dateOfBirth);
        this.empolyeeID=employeeID;
        this.designation=designation;
        this.salary=salary;
        this.dateOfJoining=dateOfJoining;
        
    }


    public String getEmpolyeeID() {
        return empolyeeID;
    }


    public Designation getDesignation() {
        return designation;
    }


    public double getSalary() {
        return salary;
    }


    public Date getDateOfJoining() {
        return dateOfJoining;
    }
}
package objects.objects;

public enum Designation {
 
    ASSOCIATE,SENIOR_ASSOCIATE,MANAGER,SENIOR_MANAGER,DIRECTOR
}

The oracle javaOracle Java documentation says : Don't share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods

Don't share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods

Does it mean i shouldntI shouldn't use getters or maybe pass a copy (a new DateDate object in the getter  ? ) or is theirthere some other approach that iI am missing?

I just finished studying immutable objects and their advantages so i thought i'd create one of my own. Here is my employee class that extends a person class. It is not mutable since i have getters to my mutable date objects.

package objects.objects;

import java.util.Date;

public class Person {
private final String name;
private final Date dateOfBirth;

Person(String name,Date dateOfBirth){
    this.name=name;
    this.dateOfBirth=dateOfBirth;
    
}
public String getName() {
    return name;
}
public Date getDateOfBirth() {
    return dateOfBirth;
}

}
package objects.objects;

import java.util.Date;

public final class Employee extends Person{

private final String empolyeeID;
private final Designation designation;
private final double salary;
private final Date dateOfJoining;


public Employee(String Name,Date dateOfBirth,String employeeID,Designation designation,double salary,Date dateOfJoining)
{
    super(Name,dateOfBirth);
    this.empolyeeID=employeeID;
    this.designation=designation;
    this.salary=salary;
    this.dateOfJoining=dateOfJoining;
    
}


public String getEmpolyeeID() {
    return empolyeeID;
}


public Designation getDesignation() {
    return designation;
}


public double getSalary() {
    return salary;
}


public Date getDateOfJoining() {
    return dateOfJoining;
}
}
package objects.objects;

public enum Designation {
 
ASSOCIATE,SENIOR_ASSOCIATE,MANAGER,SENIOR_MANAGER,DIRECTOR
}

The oracle java documentation says : Don't share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods

Does it mean i shouldnt use getters or maybe pass a copy (a new Date object in the getter  ? ) or is their some other approach that i am missing?

I just finished studying immutable objects and their advantages so I thought I'd create one of my own. Here is my employee class that extends a person class. It is not mutable since i have getters to my mutable date objects.

package objects.objects;

import java.util.Date;

public class Person { 

    private final String name;
    private final Date dateOfBirth;
    
    Person(String name,Date dateOfBirth){
        this.name=name;
        this.dateOfBirth=dateOfBirth;
        
    }
    public String getName() {
        return name;
    }
    public Date getDateOfBirth() {
        return dateOfBirth;
    }

}
package objects.objects;

import java.util.Date;

public final class Employee extends Person{

    private final String empolyeeID;
    private final Designation designation;
    private final double salary;
    private final Date dateOfJoining;
    
    
    public Employee(String Name,Date dateOfBirth,String employeeID,Designation designation,double salary,Date dateOfJoining)
    {
        super(Name,dateOfBirth);
        this.empolyeeID=employeeID;
        this.designation=designation;
        this.salary=salary;
        this.dateOfJoining=dateOfJoining;
        
    }


    public String getEmpolyeeID() {
        return empolyeeID;
    }


    public Designation getDesignation() {
        return designation;
    }


    public double getSalary() {
        return salary;
    }


    public Date getDateOfJoining() {
        return dateOfJoining;
    }
}
package objects.objects;

public enum Designation {
    ASSOCIATE,SENIOR_ASSOCIATE,MANAGER,SENIOR_MANAGER,DIRECTOR
}

The Oracle Java documentation says :

Don't share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods

Does it mean I shouldn't use getters or maybe pass a copy (a new Date object in the getter? ) or is there some other approach that I am missing?

Source Link
Ishan Soni
  • 808
  • 2
  • 11
  • 17
Loading