0

I have this generic class:

final class Box<T> {
   public T t;

   public void add(T t) {
      this.t = t;
   }

   public T get() {
      return t;
   }
}

Given that there is another class Group, I am trying the following:

Box group1=new Box<Group>();
Group group = methodThatReturnsAGroupObject();
group1.add(group);
System.out.println(group1.get().getMembers().getValue()); 

Trying to compile this throws a "cannot find symbol" error for getMembers(). If I replace group1.get() simply with group, it compiles without issue. What am I doing wrong?

EDIT: So it was explained that my first line should have been:

Box<Group> group1=new Box<Group>();

But after updating to that, I am still getting the compiler error about "cannot find sybol". So as it is no longer a raw type, what other mistake am I making?

  • At the end of "What is a raw type and why shouldn't we use it?" it says we should use Generics. I thought I was using a generic. How/Why is this a raw type issue and not a generic issue? – Peter Schweitzer Jr Feb 21 '18 at 17:11
  • You used the generic type argument in the new instance creation expression but not in the variable type. – Savior Feb 21 '18 at 17:16
  • You need to say `Box group1 = new Box()`. Otherwise the *variable* is still a raw type, the assigned value notwithstanding. – Ian McLaird Feb 21 '18 at 17:17
  • Your method invocations are done on the variable, which is of a raw type. – Savior Feb 21 '18 at 17:17
  • I updated my code to `Box group1=new Box();` and still get the same compiler error. – Peter Schweitzer Jr Feb 21 '18 at 17:46
  • But perhaps I am also tackling my issue in the wrong way. I am trying to have the variable `group` be one of two class types (Group or Role) based on a conditional. – Peter Schweitzer Jr Feb 21 '18 at 17:55

0 Answers0