Generic Class and Default constructor

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

    #1

    Generic Class and Default constructor

    hey i m new in Generic Class in java.

    look at this code carefully ....

    [code=java]
    class GenerericClass< T>
    {
    private T d1,d2;
    public GenericClass()
    {
    d1 =100;
    d2 = 200;
    }
    }
    [/code]

    another part of my code ....

    [code=java]
    GenericClass<St ring> c = new GenericClass<St ring>();
    //It will flash a compiler error .....
    [/code]

    So my conclusion is that having a default constructor in GenericClass is meaningless.
    Right????

    i think experts allof u get my point .....
    so plz explain ......

    kind regards,
    Dmjpro.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by dmjpro
    hey i m new in Generic Class in java.

    look at this code carefully ....

    [code=java]
    class GenerericClass< T>
    {
    private T d1,d2;
    public GenericClass()
    {
    d1 =100;
    d2 = 200;
    }
    }
    [/code]
    If d1 and d2 are of a generic type T you can't assume that they can contain
    numbers (Short, Integer, Long, Float or Double). That code is simply wrong and
    has nothing to do with the fact that you assume that in a default constructor,
    i.e. it is wrong everywhere in a generic class.

    kind regards,

    Jos

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by dmjpro
      hey i m new in Generic Class in java.

      look at this code carefully ....

      [code=java]
      class GenerericClass< T>
      {
      private T d1,d2;
      public GenericClass()
      {
      d1 =100;
      d2 = 200;
      }
      }
      [/code]

      another part of my code ....

      [code=java]
      GenericClass<St ring> c = new GenericClass<St ring>();
      //It will flash a compiler error .....
      [/code]

      So my conclusion is that having a default constructor in GenericClass is meaningless.
      Right????

      i think experts allof u get my point .....
      so plz explain ......

      kind regards,
      Dmjpro.
      DJ, Your GenerericClass won't compile for two reasons.
      1.) The "constructo r"'s name is different from the class' name and you hav e
      d1 =100;
      d2 = 200;
      which are not allowed since d1 and d2 are objects of a type T which is incompatible with int.

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        Originally posted by JosAH
        If d1 and d2 are of a generic type T you can't assume that they can contain
        numbers (Short, Integer, Long, Float or Double). That code is simply wrong and
        has nothing to do with the fact that you assume that in a default constructor,
        i.e. it is wrong everywhere in a generic class.

        kind regards,

        Jos

        Yup ... u r right.
        Actually wat happens .... without compiling the GenericClass i wrote what was in my mind.
        now if i want to add some default values in default constructor in generic class then what to do?
        plz suggest.

        kind regards,
        Dmjpro.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by dmjpro
          Yup ... u r right.
          Actually wat happens .... without compiling the GenericClass i wrote what was in my mind.
          now if i want to add some default values in default constructor in generic class then what to do?
          plz suggest.

          kind regards,
          Dmjpro.
          If those defaults have a fixed type then those members aren't generic (as in your
          example class). If they *are* generic all you know is that you can set them to
          something with the same generic type or null. Those numbers 100 and 200 most
          certainly aren't generic. You have to redesign your class.

          kind regards,

          Jos

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            Originally posted by JosAH
            If those defaults have a fixed type then those members aren't generic (as in your
            example class). If they *are* generic all you know is that you can set them to
            something with the same generic type or null. Those numbers 100 and 200 most
            certainly aren't generic. You have to redesign your class.

            kind regards,

            Jos

            ohh that means i have to believe in default values set by the JVM.
            right???

            kind regards,
            Dmjpro.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by dmjpro
              ohh that means i have to believe in default values set by the JVM.
              right???

              kind regards,
              Dmjpro.
              I don't see where the JVM comes into this.

              Comment

              • dmjpro
                Top Contributor
                • Jan 2007
                • 2476

                #8
                Originally posted by r035198x
                I don't see where the JVM comes into this.
                Nooo . i just say generic solved by the compiler but ultimate job done by JVM.
                that's why i telling....

                Kind regards,
                Dmjpro.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by dmjpro
                  Nooo . i just say generic solved by the compiler but ultimate job done by JVM.
                  that's why i telling....

                  Kind regards,
                  Dmjpro.
                  If you want a member variable to be equal to 100 or 200, that particular member
                  variable can't be part of the generic contract. It simply is a number.

                  kind regards,

                  Jos

                  edit: here's a nice tutorial.

                  Comment

                  Working...