-2

Im not going to paste my whole entire code here for my problem. To keep things simple ill just make up some code right here for my problem.

Okay i have a class called "exampleClass" that has an integer variable called "number" for example and "number" has a get and set method. My question is: If i have multiple objects of "exampleClass" how do i get a specific object out of the multiple objects so i can access that specific object from inside "exampleClass".

"exampleClass" below

   public class exampleClass{

    public int number;

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }
}
  • 1
    ` If i have multiple objects of "exampleClass" in a different class how do i get a specific object out of the multiple objects inside the "exampleClass" so i can access that specific object from inside "exampleClass".` Please clarify cause it makes no sense :) – nem035 Sep 20 '14 at 17:41
  • Your question is a bit vague, but you may want to use a [`HashMap`](http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html), assuming each object has a different number and you know what those numbers are. – Lone nebula Sep 20 '14 at 17:56

3 Answers3

1

Assuming number is what makes any instance of your class "unique" for the purpose of comparison, you should consider overriding the equals and hashCode methods.

That way, you'll be able to find instances of your class within a collection such as an ArrayList using indexOf(Object o);

For example:

public class ExampleClass
{
    private int number;

    public ExampleClass(int number)
    {
        this.number = number;
    }

    public int getNumber()
    {
        return number;
    }

    public void setNumber(int number)
    {
        this.number = number;
    }

    @Override
    public boolean equals(Object other)
    {
        boolean isEqual = false;

        if (other instanceof ExampleClass)
        {
            ExampleClass otherEC = (ExampleClass)other;

            isEqual = number == otherEC.number;
        }

        return isEqual;
    }

    @Override
    public int hashCode()
    {
        return number;
    }
}

and

public static void main(String[] args)
{
    List<ExampleClass> list = new ArrayList<ExampleClass>();

    ExampleClass ec1 = new ExampleClass(1);

    list.add(ec1);
    list.add(new ExampleClass(3));
    list.add(new ExampleClass(102));

    System.out.println(list.indexOf(new ExampleClass(3)));
    System.out.println(list.indexOf(new ExampleClass(1)));
    System.out.println(list.indexOf(ec1));
    System.out.println(list.indexOf(new ExampleClass(5)));

}

Will produce the following output:

1
0
0
-1

Please look here for more information on why you should override equals and hashCode for objects that you want to store in collections:

Why do I need to override the equals and hashCode methods in Java?

Community
  • 1
  • 1
Michael Krause
  • 4,689
  • 1
  • 21
  • 25
0

Is this what you want to achieve?

import java.util.HashMap;

public class Test {
    public static void main(String[] args) {
        new ExampleClass(123);
        new ExampleClass(456);

        ExampleClass obj1 = ExampleClass.getInstance(123);
        ExampleClass obj2 = ExampleClass.getInstance(456);
        ExampleClass obj3 = ExampleClass.getInstance(789);

        System.out.println(obj1); //Output: ExampleClass-object with ID 123
        System.out.println(obj2); //Output: ExampleClass-object with ID 456
        System.out.println(obj3); //Output: null
    }
}
class ExampleClass {
    private static final HashMap<Integer, ExampleClass> instances
            = new HashMap<Integer, ExampleClass>();

    private final int id;

    public ExampleClass(int id) {
        if(instances.get(id) != null)
            throw new IllegalArgumentException("An instance with ID " + id
                    + " already exists.");
        this.id = id;
        instances.put(id, this);
    }
    public int getID() {
        return id;
    }
    public static ExampleClass getInstance(int id) {
        return instances.get(id);
    }
    @Override
    public String toString() {
        return this.getClass().getSimpleName() + "-object with ID " + id;
    }
}

Please note that in the example above, making two ExampleClass-objects with the same ID will throw an exception.

Lone nebula
  • 4,768
  • 2
  • 16
  • 16
0

You need to associate a unique identifier to each object that you create. The closest you can get to that without changing the class structure is using hashcode. The uniqueness of objects is not guaranteed in this case though. So the next option is to change the structure of the class (which is bad idea) and add a UUID to the class. Initialize the UUID when the object is created (constructor). To keep track of the object now, you can use a HashMap.

SJha
  • 1,510
  • 1
  • 10
  • 17