The type is a ClassName in generics

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    The type is a ClassName in generics

    I have this code ..
    Please have a look...
    Code:
    class GenericClass<Number>{
        Number number = null;
        GenericClass(Number number){
            this.number = number;
        }
    }
    
    GenericClass<String> gc = new GenericClass(100); //It gets compiled
    System.out.println(gc.number); //it leads me to a [B]ClassCastException[/B]
    The Number is a type or ClassName?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Doesn't your compiler give you a warning when you compile that? Read that warning.

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      I didn't see that warning. So here Number is treated as type name?
      Why warning, why not error?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Why should it be an error? You are allowed to hide those classes at your own peril.

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          Originally posted by r035198x
          Why should it be an error? You are allowed to hide those classes at your own peril.
          If i add a method like this ..

          Code:
          void add(Number number){this.number=number;}
          and then if i use this, then it flashes me an error ..

          Code:
          gc.add(200); //it shows compile error.
          Why in case of constructor it shows a warning and in case of method it shows error?

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Post the code that you used and the error that you got.

            P.S You'd better read the generics tutorial first before trying to use generics.

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              Originally posted by r035198x
              Post the code that you used and the error that you got.

              P.S You'd better read the generics tutorial first before trying to use generics.
              Here is my Code ...
              Code:
              class GenericClass<Number>{
                  Number number = null;
                  GenericClass(Number number){
                      this.number = number;
                  }
                  void add(Number n){
                      this.number = n;
                  }
              }
              
              public class Main {
                 public static void main(String[] args) {
                      GenericClass<String> gc = new GenericClass(100);//it does not show me error
                      gc.add(200); //it shows me error
                  }
              }

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by dmjpro
                Here is my Code ...
                Code:
                class GenericClass<Number>{
                    Number number = null;
                    GenericClass(Number number){
                        this.number = number;
                    }
                    void add(Number n){
                        this.number = n;
                    }
                }
                
                public class Main {
                   public static void main(String[] args) {
                        GenericClass<String> gc = new GenericClass(100);//it does not show me error
                        gc.add(200); //it shows me error
                    }
                }
                You do realize that the class java.lang.Numbe r is hidden by that generic class don't you? Autoboxing goes berzerk and a general mess will be yours.

                Better stay far away from that dangerous crap if you don't know what you are doing.

                kind regards,

                Jos

                Comment

                • dmjpro
                  Top Contributor
                  • Jan 2007
                  • 2476

                  #9
                  See ... What i realized..

                  Code:
                  class GenericClass<T>{
                  GenericClass(T t){...}
                  }
                  
                  GenericClass<String> gc = new GenericClass(/*Any type of object*/);
                  
                  GenericClass<String> gc = new GenericClass<ClassName>(/*Only ClassName type of object*/);

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    The first line in your main should give you another warning as well.
                    You can't call add with numbers because you specified your type to be String. You can only add Strings to it. Better use the conventions (T for type and E for element) when using generics.

                    Don't forget to read that generics tutorial.

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by r035198x
                      Better use the conventions (T for type and E for element) when using generics.

                      Don't forget to read that generics tutorial.
                      IMHO it would've been much better to explicitly forbid an existing class name to be used as a generic type name; some poofters might try to come up with MyClass<String> or MyClass<Number> and be surprised that some stuff won't work as expected, especially when they want to use those (hidden) class names from within the definition of their crappy generic class.

                      kind regards,

                      Jos

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        I thought as much as well the first time I saw this thread. They did put the warning though. Now they need to get people to read those warnings.

                        Comment

                        Working...