See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

How can I prevent multiple instances of an object from having the same attributes in Java

General Tech Bugs & Fixes
Max. 2000 characters

Raman Tripathi


( 8 months ago )


Below is my code snippet

Account acc1 = new Account ("123", "James", "Savings");
Account acc2 = new Account ("234", "James", "Checking");
acc1.setNickName("James Account");
acc2.setNickName("James Account");

//Nick Name Method
private String nickName;

public void setNickName (String name)
    nickname = name;


My question is how can I prevent acc2 object from having the value of NickName same as that of acc1?

I want to ensure that in the setNickName method there is a mechanism to prevent two object instances from being the same.

Rahul Chaudhary


( 8 months ago )

It's not as simple as just overriding equals or hashCode in your Account object. By specifying a constraint on two distinct instances of Account, this implies that both instances are aware of one another. In this context, they're not going to be; they're two independent instances which really shouldn't be aware of each other in reality.

What you're going to have to do is have some kind of account manager service, which could then be responsible for setting the nickname on the accounts.

In this context, you don't really have to override either equals or hashCode since that's not going to give you what you want anyway; you won't always have two otherwise identical accounts with the same nickname, and bastardizing the equals method to pay attention to a single field feels wrong.

First things first - let's start with a simple account manager. This makes use of collections and streams, concepts which for brevity I'll leave as an exercise for the reader.

The mechanics are straightforward:

  • If we have an account which has the nickname, we don't update it. If we don't, we update it by ID.
  • This service is now responsible for creating and manipulating all accounts.  


final List<Account> accounts = new ArrayList<>();

public Account createAccount(String id, String name, String type) {

Account account = new Account(id, name, type);


return account; }

public boolean setNickName(String id, String nickName) {

Optional<Account> existingAccount =

.firstMatch(a -> a.getNickName().equals(nickName));

if(existingAccount.isPresent()) {

return false; // don't allow modification to an account who has the same nickname

} else {

.firstMatch(a -> a.getId().equals(id))

.ifPresent(a -> a.setNickName(nickName));

return true;




Now, with this class, you should get what you want - any account managed by the AccountManager won't be able to have the same nickname.

Priya Roy


( 8 months ago )

You can create a static Set object in your Account class and every time a nickname is added, you check if already one exists. If it doesn't, then set the value. If it doesn't exists, then you can have your own logic(I have thrown a IllegalArgumentException)

class Account {

private static Set<String> nickNameSet = new HashSet<>();

public void setNickName(String nickName) {
    this.nickName = nickName;
    else {
        throw new IllegalArgumentException("Nick Name already exists");

As pointed out by @Makoto, defining the Set object in the Account class will lead to all account object having access to nickname of other accounts. If data hiding is your concern, we should create a new class say AccountManager and have the logic of identifying duplicate nickName delegated to it.

what's your interest