• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generics: cannot make a static reference to the non-static type (continued)  RSS feed

 
Aahan Agrawal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A similar question was previously answered. Greenhorns might jump at my question with the claim, as a result, that it was already answered. I do not believe that the previous forum's results (http://www.coderanch.com/t/517280/java/java/Generics-static-reference-static-type) answer my question though.


I previously define a generic class MyLinkedList <E>. Why is it illegal to define the static method .




To answer my question, I tried to apply the pasted forum's findings.

Your problem isn't with Generics...your problem was trying to reference a non-static inner class from a static method. You always need an instance of the outer class class to make an instance of a non-static inner class.


I can't fathom why however, my method block involves the use of a non-static inner class. Even if MyLinkedList<E> were somehow an inner class, passing it as a parameter in the method block isn't illegal. No restrictions are ever placed on what can be passed into a static method.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hint: Can you tell us what E is (at line 7)? And where is it declared (or defined)?

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Hint: Can you tell us what E is (at line 7)? And where is it declared (or defined)?


BTW, since the OP never responded, I am going to assume that the OP figured out that this was simply a case of not having an E type variable in scope.


As for the weird compiler message, I believe that the compiler assumed that it referred to the E type variable defined at line 3. Of course, this required an instance of the LinkedListTestor class, which doesn't exist in the scope of a static method.

If I had to speculate, I think that the OP meant to have a generic method, so the compiler error message was very confusing. Oh well.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!