0

I have 2 interfaces Sub and Obs as illustrated below:

public interface Sub<O extends Obs<? extends Sub>>{
   public void addObs(O);

   public void removeObs(O);
}

public interface Obs<S entends Sub<?>>{
   public void update(S);
}

Now there are 2 concrete implementations of the above as class Vie which implements Obs and class Mod which implements Sub as below:

public class Vie<M extends Mod> implements Obs<M>{
   public void update(M){
      //some code;
   }
} 

public class Mod implements Sub<Vie<Mod>>{

   Vie<Mod>[] obs = new Vie<Mod>[0]; //This is the line of error.

   public void addObs(Vie<Mod>){
     //some code;
   }

   public void removeObs(Vie<Mod>){
      //some code;
   }
}

Now my problem is when I want to initialise the array of Vie in the class Mod I get an error "cannot create generics array of Vie<Mod>". So how can I initialise an array of Vie<Mod> containing 0 elements.

NOTE I know of a solution:

@SuppressWarnings("unchecked")
Vie<Mod>[] obs = (Vie<Mod>[]) new Vie<?>[0];

I am not looking for this can anybody suggest anything else?

Edit I have already posted the answer that was there in the question which is attached as duplicate to this question. I was not looking for answers like that. I can share this another question like mine. But my point is that is there any other way?.

Now I add a second part to my question:

If no other solution, following my suggested solution, are there any problems in this casting as it would not be checked at the compile time. Is there any scope or any runtime exceptions?

Community
  • 1
  • 1
Blip
  • 2,711
  • 3
  • 17
  • 42
  • 1
    Use a `List` instead of raw arrays. –  May 16 '15 at 14:35
  • The solution you showed in the question is the only way. (Or use a List like @Jarrod says.) – Radiodef May 16 '15 at 14:38
  • 1
    @Blip Your question boils down to "the compiler is telling me I can't do this, I know there is a workaround but I don't like it and I want to do what the compiler says I can't do". – Radiodef May 16 '15 at 14:40
  • @Radiodef are there any problems in this casting as it would not be checked at the compile time. Is there any scope or any runtime exceptions? – Blip May 16 '15 at 14:41
  • @Blip it also surprised me. your question looks fine (it already has an answer, but that doesn't reflect negatively on your question) +1 – Erwin Bolwidt May 16 '15 at 14:41
  • 2
    for future reference [asking anonymous down voters for an explanation is not constructive](http://meta.stackoverflow.com/questions/252826/is-asking-reasons-for-downvote-in-comments-non-constructive), there is a perfectly good reason for the down vote if you hover over the down vote arrow. *This question does not show any research effort; it is unclear or not useful*. Duplicates fall afoul of 2 out of 3 of these explanations. No research effort **and** not useful. *"How can I improve my question?"* is a different type of comment. –  May 16 '15 at 14:44
  • No, casting the array won't cause a problem. The creation is not allowed because the unchecked `Vie[]` doesn't behave as an array. Arrays throw an exception if you put wrong elements in them, and `Vie[]` will not throw an exception if someone converts it back to `Vie[]` or `Vie>[]` and puts e.g. a `Vie` in it. – Radiodef May 16 '15 at 14:45

0 Answers0