-2
  public Spaces(int a,String b,String c){//Constuctor for Dinos.
    String ds= "d" + a;
    Dino ds = new Dino(a,b,c);
  }

Consider that I want to create classes named d1,d2,d3,d4. How can I do this?

2 Answers2

0

Consider adding a "name" instance variable to Dino

class Dino {

    private String name;

    private int a;
    private String b;
    private String c;

    public Dino(String name, int a, String b, String c;) {
        this.name = name;
        this.a = a;
        this.b = b;
        this.c = c;
    }

    // ...
}

You could then add some Dinos to a list:

List<Dino> dinos = new ArrayList<>();

for (int i = 1; i <= 4; i++) {
    dinos.add(new Dino("d" + i, 1, "2", "3"));
}

Then retrieve the ones you want later:

Dino d2 = dinos.stream()
    .filter(d -> d.getName().equals("d2"))
    .findFirst()
    .orElseThrow(() -> new RuntimeException("Dino not found"));
4dc0
  • 433
  • 3
  • 11
0

That's not possible in the way you're thinking, but instead of that you can use a simple array.

Dino[] dinos = new Dino[4];
dino[1]= new Dino(a,b,c); //e.g. of array use

Here you can find more info about array declaration. Please also consider to learn to use data structure before doing anything in your code ( for example you can learn here how to use arrays).

Thecave3
  • 681
  • 11
  • 24