0
public class Main<T> {
     T obj;
     public Main(T input) {
         this.obj = input;
     }
     void set(T input) {
         this.obj = input;
     }
     void print() {
         System.out.println(this.obj);
     }
    public static void main(String[] args) {
        Main<Integer> tester = new Main<>(2);
        Main test = tester;
        test.print();
        test.set(3);
        test.print();
    }
}

In the code above test.set(3) gives me a warning "Unchecked call to 'set(T)' as a member of raw type 'Main'". What is an unchecked call and why do I get it, even though the set method works and after the print statement is executed, 3 is printed.

Rahat
  • 305
  • 1
  • 4
  • 10
  • 2
    See [What is a raw type and why shouldn't we use it?](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – Bohemian Feb 18 '20 at 00:22
  • You should have a compiler warning for `Main test = tester;`, telling you that you have used a raw type. In general, you want to look at the first warning or error in your code, as it often contributes to the later warnings and errors. – VGR Feb 18 '20 at 01:26

2 Answers2

2

You haven't told the compiler what kind of Main the test variable refers to. As far as the compiler is concerned, it could be Main<Integer> or Main<String> or Main<RahatsCrazyClass>. So the compiler can't guarantee that test.set(3) makes sense - you could be trying to set an Integer object inside a Main<String>.

The warning tells you that the compiler has encountered a condition that it can't guarantee the sense of. You should avoid having this kind of thing. Better to declare test as a Main<Integer> than as just a Main.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
1

You are explicitly defeating the purpose of Generics, which implicitly specializes your generic class with Object type argument. In this case, you're defeating type-safety and setter method on the raw type Generic will be unchecked in compile-time, due to late binding. Compiler simply does not know at compile time what type of value is passed into setter method, as there is no type checking.

You should always try not to use raw type specialization in your Generic classes, unless it is really must to do.

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66