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 .

Arash
  • 43
  • 4
  • 1
    a `Map` like can be used; see https://docs.oracle.com/javase/8/docs/api/java/util/Map.html – fantaghirocco Jul 18 '18 at 13:59
  • Return the `Things` instance from where? You have nothing that *contains* `apple` and `dog`. –  Jul 18 '18 at 14:01
  • Possible duplicate of [Storing a new object as the value of a hashmap?](https://stackoverflow.com/questions/12099843/storing-a-new-object-as-the-value-of-a-hashmap) – LuCio Jul 18 '18 at 14:08
  • @intentionallyleftblank I edited my question . – Arash Jul 18 '18 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. –  Jul 18 '18 at 14:33

3 Answers3

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?

Thiyagu
  • 17,362
  • 5
  • 42
  • 79
  • 1
    Maybe remove the `int id` parameters from `addThing`. The `Things` has the `ID` member, only the getter is missing. –  Jul 18 '18 at 14:05
  • 1
    @intentionallyleftblank Of course. Modified my answer. Thanks – Thiyagu Jul 18 '18 at 14:07
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, ...)
GhostCat
  • 137,827
  • 25
  • 176
  • 248
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.

Ernesto
  • 950
  • 1
  • 14
  • 31