Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9205561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:58:43+00:00 2026-06-17T23:58:43+00:00

I am having a slight inconvenience when working with generics in Java. Please consider

  • 0

I am having a slight inconvenience when working with generics in Java. Please consider the following code:

/**
 * MyElement class is simply a wrapper for a generic object.
 */

public static class MyElement<T> {
    public final T OBJ;

    public MyElement(T obj) {
        this.OBJ = obj;
    }
}

/**
 * MyElementList contains an array list of MyElements of the given type, T.
 * This represents a class that uses a list of MyElements of a certain type,
 * and this list can be accessed in an unmodifiable format.
 */

public static class MyElementList<T> {

    //Properties
    private List<MyElement<T>> elementList = new ArrayList();

    //CTOR
    public MyElementList(List<MyElement<T>> initElements) {
        elementList.addAll(initElements);
    }

    //Getter
    public List<MyElement<T>> getElements() {
        return Collections.unmodifiableList(elementList);
    }

}

public static void main(String[] args) {
    //New list of elements
    //Notice that I did not explicitly specify the type for 'MyElement'
    List<MyElement> theElements = new ArrayList(Arrays.asList(
                new MyElement[] {
                    new MyElement("E 1"),
                    new MyElement("E 2"),
                    new MyElement("E 3")
                }
            ));

    //Also notice I did not explicitly specify the type for 'MyElementList'
    MyElementList theList = new MyElementList(theElements);

    //The following does not work.
    //It seems to not work because theList.getElements() returns a 'List'
    //not necessarily a 'List<MyElement>' which is what I would expect it to
    //return...
    //Why???

    for(MyElement e : theList.getElements()) {
        System.out.println(e.OBJ.toString());
    }

    //Currently my work around is to do the following, but I do not like 
    //having to introduce another variable, and I would rather just do the 
    //one above

    List<MyElement> listOfElements = theList.getElements();
    for(MyElement e : listOfElements) {
        System.out.println(e.OBJ.toString());
    }

    //How come the first 'for-each' loop method does not work?
    //Is there anyway I could get it to work?
    //THANK YOU!
}

In the main method, if I don’t specify the type parameter for ‘MyElementList’ the ‘getElements()’ method only returns a ‘List’, not a ‘List<MyElement>’. This is inconvenient because if I want to iterate through each ‘MyElement’ I need to introduce another variable as a temporary list, shown in the code.

  • Why doesn’t the ‘getElements()’ method return a ‘List<MyElement>’?
  • Without making significant changes to ‘MyElementList’ Is there anything I can do to fix this?
  • Is this a bad design practice?

The IDE I am using is Netbeans 7.2

Thanks in advance!

EDIT

Thank you all for your quick responses. I am very impressed with the community here. I have concluded the following:

  • If a generic hint is not specified, Java ignores ALL other associated generic hints for a class – which is kind of lame, but I can live with it.
  • When using generics, it is a best practice to actually specify the generic type when creating an instance of the class. This seems to be the most object oriented solution.

Thanks again!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:58:45+00:00Added an answer on June 17, 2026 at 11:58 pm

    If you change MyElementList to look like

    public static class MyElementList<T extends MyElement> {
    
      //Properties
      private List<T> elementList = new ArrayList<T>();
    
      //CTOR
      public MyElementList(List<T> initElements) {
          elementList.addAll(initElements);
      }
    
      //Getter
      public List<T> getElements() {
          return Collections.unmodifiableList(elementList);
      }
    }
    

    It should work.

    EDIT Generics can be seen as compile time hints in Java, since Java erasure will convert generics to Object. Updating your class as above will tell the compiler only elements which extend MyElement fit the list and for(MyElement e : theList.getElements()) will work.

    EDIT 2 As pointed out by others (sorry, I didn’t see it at first glance) also change the raw declaration to:

    MyElementList<MyElement> theList = new MyElementList<MyElement>(theElements);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a slight performance issue with inline function. Consider the following code:-
Having a slight regex problem. I wrote to following code to check if a
I am having some slight difficulty with the following code: Lagrange[list_] := Module[{points =
Having a slight problem with the following block of code: newusr = c.readLine(New user?
I'm having a slight problem with my SDL/Opengl code, specifically, when i try to
I'm having slight difficulty in performing a calculation in Java. Here is what I'm
I'm working on a simple framework, and I'm having a slight problem. I'd like
I'm having some slight problems with fading one div into another, here's my code
I am having som slight difficulties with the following problem. I have initialized a
I'm using Entity Framework 4.1 code-first and I'm having a slight issue with retriving

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.