0

I created a class and made 57 objects from it, each one has specific ID number.

Can I create a method which returns an object using an ID as the argument?

For example, assume the name of my class is Things and I made two object from it called apple and dog, they have IDs 1 and 2.

Things.java:

class Things {

    private String name;
    private int ID;

    public Things(String name, int ID) {
        this.name = name;
        this.ID = ID;
    }
}

Main.java:

class Main {

    public static void main(String[] args) {
        Things apple = new Things("apple", 1);
        Things dog = new Things("dog", 2);
    }
}

in this example I want to create a method in class "Things" which returns object apple if I use 1 as argument and object dog if I use 2 .

5
  • 1
    a Map<Integer,Things> like <id,object> can be used; see docs.oracle.com/javase/8/docs/api/java/util/Map.html Commented Jul 18, 2018 at 13:59
  • Return the Things instance from where? You have nothing that contains apple and dog. Commented Jul 18, 2018 at 14:01
  • Possible duplicate of Storing a new object as the value of a hashmap? Commented Jul 18, 2018 at 14:08
  • @intentionallyleftblank I edited my question . Commented Jul 18, 2018 at 14:20
  • What you ask for in your edit makes no sense. In fact, the name of your class is wrong. You don't have Things but two instances of Thing: apple and dog. Each Thing only knows itself, it has no idea that there is a second Thing. Use the ThingRepository approach outlined in the answer below. Commented Jul 18, 2018 at 14:33

3 Answers 3

3

You cannot identify objects by a particular property unless you store it in a special repository

You can create a ThingRepository and can get specific Things by the id.

public class ThingRepository {
   private Map<Integer, Things> thingsRepository = new HashMap<>();

   public void addThing(int id, Things things) {
      thingsRepository.put(id, things);
   } 
   public Things getThingById(int id) {
       return thingsRepository.get(id); //Can return null if not present
   }
}

The addThing method need not explicitly take the id. If you add a getter to Things, then it can be simplified to

public void addThing(Things things) {
    thingsRepository.put(things.getId(), things);
} 

Couple of problems you need to address:

  1. Each created Things object has to be added to this somehow (either the caller needs to add or there must be some other wrapper/factory that must do this).
  2. Once a Things is not needed, it must be removed from the above map, else it can lead to memory leak.

Btw, shouldn't Things be named as just a Thing?

Sign up to request clarification or add additional context in comments.

2 Comments

Maybe remove the int id parameters from addThing. The Things has the ID member, only the getter is missing.
@intentionallyleftblank Of course. Modified my answer. Thanks
1

There are two aspects here:

  • you need some sort of data structure that remembers about created objects, and allows you to access them by id, for example a simple Map<Integer, Things>. Each time you create a new Things (should better be called Thing, shouldn't it?!), you go thatMap.put(newId, newThing).
  • if you want that data to "survive", you would have to somehow persist it (like writing data to a file, database, ...)

Comments

0

If you use Intellij for example press: alt + insert and choose getters/setter.

If not just write your own getters/setter ;).

Like here: https://docs.oracle.com/javaee/6/tutorial/doc/gjbbp.html

But basically if you want to look for Thing with particular Id you need to store somewhere them for example in ArrayList, then iterate through it and if your find element with that Id just return it.

1) Create new ArrayList

2) Iterate through

3) If you find Thing with Id you want, return it.

5 Comments

A setter is not useful to get an instance using the ID as the argument.
@intentionallyleftblank that's true
Yes, using some kind of container is possible. You could edit your answer and explain this approach.
This doesn't answer the question. He doesnt want to know how to generate getters and setters.
Iterating of a List is a bad answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.