Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Generics

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Generics

    I'm having trouble figuring out how to properly define Generics. Could you let me know what I'm doing wrong here?

    public class MyClass1<T>
    {
      private final MyClass2<T> myobject;
      private final T startValue;
     
      public MyClass1(T value, MyClass2<T> obj)
      {
        startValue = value;
        myobject = obj;
      }
      public void run()
      {
        for ( T i = startValue; i < startValue+3; i++ )
          myobject.run(i);
      }
    }

    Then I call it with this:
    MyClass1<int>  myclass1i = new MyClass1<int> ( 1 , myObj);
    MyClass1<char> myclass1c = new MyClass1<char>('a', myObj);

    I'm sure there are some syntax mistakes, but there is also the problem that i++ isn't recognized. This would be easy in C++ as I'd just have to ensure that any classes which act as the template type need to have ++ overloaded (int and char do support these). I'd only get a compiler error if I used an object which doesn't support this. I'm not sure that it's this easy in Java.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Generics

    Primitives (int, char, double, etc.) can't replace generic class variables. Use the wrapper classes if you must.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Generics

    That is certainly too bad. Thanks for the response.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Generics

    Don't be discouraged. The wrapper classes, Integer, Double, Character, etc. should allow you to do what you need. Or, perhaps you don't need generics at all. Maybe you can achieve the desired result by simply overloading some methods and/or constructors.

Similar Threads

  1. What am i doing wrong? Generics
    By gberisha in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 13th, 2013, 02:42 PM
  2. Need some help with Generics
    By bankston13 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 5th, 2012, 03:06 PM
  3. Generics.
    By Kumarrrr in forum Java Theory & Questions
    Replies: 1
    Last Post: December 6th, 2011, 06:53 PM
  4. Generics
    By Kerr in forum Collections and Generics
    Replies: 2
    Last Post: May 19th, 2011, 06:44 PM
  5. Generics
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 21
    Last Post: December 6th, 2010, 07:08 PM