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 2 of 2

Thread: class type paramaters

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default class type paramaters

    So am getting comfortable with generics as I go trough my Thinking in java book. I understand concept and how easier things can be with generics, like when creating constructor , method or even a field. But I just can not understand why do in most of examples they are in the class declaration like : public class LinkedStack<T> {}. please help me understand


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: class type paramaters

    It doesn't make much sense to use instance-level generics without a class-level generic declaration.

    For example:

    class ClassA
    {
        T b;
     
        T getB(T param)
        {
            return b;
        }
    }

    Is field b expected to be a generic type, or is there actually class T? There's no way to tell. You must specify a class generic in order to clarify this.

    class ClassA<T>
    {
        T b;
     
        T getB(T param)
        {
            return b;
        }
    }

    Now it's understood that T is a generic type, and every instance of T should be replaced by the actual type.

Similar Threads

  1. having problem declaring a generic type array in OrderSet class
    By mia_tech in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 10th, 2012, 06:39 PM
  2. Paramaters to update properties file
    By d0nal in forum Java Theory & Questions
    Replies: 1
    Last Post: September 14th, 2011, 11:17 AM
  3. abstract class & 'covariant' return type?
    By jezza10181 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 12th, 2011, 02:26 PM
  4. XML InputSource class and the retrieval/browser type
    By dave0110 in forum Object Oriented Programming
    Replies: 2
    Last Post: December 3rd, 2010, 08:11 AM
  5. how to load a class and type cast to that class?
    By chinni in forum Object Oriented Programming
    Replies: 2
    Last Post: November 9th, 2009, 09:18 AM