Reputation: 230
Well, I'm not sure if I can do this but let me ask the question anyway...
I got class Budynek which constructor is like this:
public Budynek(int numerBudynku)
{
this.id.nrBudynku = numerBudynku;
this.id.nrPietra = 0;
this.id.nrPokoju = 0;
}
so I am creating it like this:
Budynek Budynek1 = new Budynek(1);
Budynek Budynek2 = new Budynek(2);
Now I want to ask if there is any way to create a method that create new Budynek for me? Lets say if I use switch case, and case 1 will be "create new Budynek"
then I would like this method to do something like this
e.g licznik = 1
Budynek Budynek+licznik(so it will be Budynek1) = new Budynek(licznik)
then just licznik = licznik + 1;
is that possible?
Upvotes: 0
Views: 86
Reputation: 37604
You can create a List and store everything there, since your are indexing anyways with budynek+index
List<Budynek> budynekList = new ArrayList<Budynek>();
budynekList.add(new Budynek(10));
budynekList.add(new Budynek(20));
budynekList.add(new Budynek(400));
budynekList.get(index); // now you have your budynek objects with the given index.
As in the comments already pointed out, you can't declare variables on runtime.
Upvotes: 2
Reputation: 109577
Either
enum Budynek {
BUDYNEK_A,
BUDYNEK_B,
BUDYNEK_C;
public ID id;
private Budynek() {
this.id.nrBudynku = 1 + ordinal();
this.id.nrPietra = 0;
this.id.nrPokoju = 0;
}
}
Budynek.BUDY_A.id.nrBudynku
or you must go for an array:
Budynek[] budyneki = new Budynek[3];
...
budyneki[0].id.nrBudynku
budyneki[1].id.nrBudynku
budyneki[2].id.nrBudynku
Upvotes: 0