Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can one control the identity of objects in Java, and if so, how?

This is a trivial programming question. I am not an expert in Java. Say I use objects of custom classes Company and Employee, in a manner similar to what many RDBMS examples do:

class Employee
{
    Company company;
}

class Company
{
    String name;
}

I need to guarantee that different Company objects have unique names - i.e. no two such objects may have the same name, because from my point of view it makes no sense, and also simply eats memory - if two employees work at IBM, then there is a single Company object with that name, period.

My thoughts right now go along of making Company constructor private - so that the job of allocating Company objects with arbitrary names is delegated to a trusted method - which, suppose, will reject any subsequent attempt to create an object with a name that already exists or return an existing or new object (creating one if necessary).

The problem is, I am not sure how to accomplish this elegantly. One thing that would be nice is not having to do a O(n) lookup every time an Company object with a name is requested - so maybe a hash map or a binary tree is there for my convenience? I would also like to override the way the Company objects are identified - which leads me to this: will I be overriding Object.equals and/or Object.hashCode methods?

like image 787
amn Avatar asked Sep 27 '10 21:09

amn


People also ask

What is the identity of an object in Java?

The state of an object is a value from its data type. The identity of an object distinguishes one object from another. It is useful to think of an object's identity as the place where its value is stored in memory.

What is the identity of a class in Java?

Each class has an associated instance ( Object. class for the Object class). This instance is unique per class loader. This means that class do have a unique identity inside a class loader.

Does equality guarantee identity?

Of course this includes also sharing the same memory address but in general while identity is related to the attributes of the object, equality is used to check whenever two objects are identical, but this doesn't include identity.

Are there objects in Java?

Java is an object-oriented programming language, meaning everything in Java is an object. Each object has a different name, and a class is unique in that they are used to create blueprints for objects. A class must have a unique name used to create individual class instances.


3 Answers

Take a look at the flyweight pattern.

What I would do is something like:

// incomplete, but you get the idea hopefully
CompanyFactory
{
    private Map<String, Company> companies;

    public getCompany(final String name)
    {
        Company company;

        company = compaines.get(name);

        if(company == null)
        { 
            company = new Company(name);
            companies.put(name, company);
        }

        return (company);
    }
}
like image 129
TofuBeer Avatar answered Sep 29 '22 13:09

TofuBeer


You could override equals and hashCode and store them in a HashMap or HashSet.

like image 45
Fabian Steeg Avatar answered Sep 29 '22 12:09

Fabian Steeg


One thing that would be nice is not having to do a O(n) lookup every time an Company object with a name is requested - so maybe a hash map or a binary tree is there for my convenience?

That sounds right, yes.

I would also like to override the way the Company objects are identified - which leads me to this: will I be overriding Object.equals and/or Object.hashCode methods?

If you ensure that there are never two instances with the same key value, you actually don't have to do that.

like image 40
Michael Borgwardt Avatar answered Sep 29 '22 12:09

Michael Borgwardt