*
The moose likes Beginning Java and the fly likes Generics: cannot make a static reference to the non-static type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generics: cannot make a static reference to the non-static type" Watch "Generics: cannot make a static reference to the non-static type" New topic
Author

Generics: cannot make a static reference to the non-static type

Victor Thomas
Greenhorn

Joined: Oct 31, 2010
Posts: 9
Okay, I'm just really confused right now... the original signature for the method I'm supposed to write :



So silly me trying to show off figured...



Of course this went over like a lead zeppelin, so...



I "think" I have the correct algorithm to reverse the list, I just need to get past this. After googling around I figured that making the method generic would solve most of the problem... LinkedListNode<T> is an inner class of the abstract class LinkedListClass. I could really use some assistance and a good canonical source with examples on generics since this text sucks on the subject. Thanks.

v
Victor Thomas
Greenhorn

Joined: Oct 31, 2010
Posts: 9
finally googled around some more... I probably shouldn't do it this way, however I'm almost out of time and need to submit this as is. However, it does works...

Complete code:









If someone has a better solution, please post it! Again, if anyone could suggest a good tutorial on generics, I'd really appreciate it. If no one replies by tomorrow, I'll mark this as resolved. BTW, this is not the way I'd do an implementation of a Linked List ... Thanks in advance.

v
Victor Thomas
Greenhorn

Joined: Oct 31, 2010
Posts: 9
Okay, I think I have something better now, (at least eclipse quit complaining...)


And...

Don't know what I did right, but at least now I can safely submit the code. I really do wish someone could explain to me the why this is 'better' than my original. I will still wait to mark this as resolved.

v
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well, you have about 90 lines of code in each of them. I could scroll back and forth and try to identify the differences, but that's awfully difficult and inconvenient. And you never really did say what you didn't like about the first attempt (and by the way, that stat/non-static issue has nothing to do with generics, and vice versa). So I'm sorry, I'm not going to do that.

On the other hand if you explained what you did, somebody might be able to explain why that was a good thing to do.
Victor Thomas
Greenhorn

Joined: Oct 31, 2010
Posts: 9
Paul Clapham wrote:Well, you have about 90 lines of code in each of them...

Sorry for the amount of code contained herein. Like I expressed earlier, I had to add code to an existing project. I would never write this of my own free will...
Paul Clapham wrote:And you never really did say what you didn't like about the first attempt

What I was concerned with was the what I thought was the problem which happened to by the real problem as you surmised: scope
Paul Clapham wrote: (and by the way, that stat/non-static issue has nothing to do with generics, and vice versa).

I was just watching the tube and realized that the issue was that LinkedListNode is an inner class and that's why it worked when I :

So my original mistake was try to instantiate the inner class. Once I finished kicking myself in the butt, I decided that I shouldn't wait until almost the last minute to complete an assignment again.
Paul Clapham wrote:So I'm sorry, I'm not going to do that.

No worries! I sometimes wear myself down and don't see the problem until I let go. I always hope that a better pair of eyes can spot my insanity quicker than me. I just need to manage my time better...Thanks for your help.
Alex Hurtt
Ranch Hand

Joined: Oct 26, 2010
Posts: 98
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. This can be implicitly done within instance methods because of the 'this' reference...but static code has no 'this' reference...hence to construct an instance of a non-static inner class from a static method you'd have to construct a new outer class first and use that to construct the inner class along the lines of:

Outer.Inner innie = new Outer().new Inner();

(where Inner is a non-static inner class inside Outer and both Outer and Inner are visible to the calling code).
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generics: cannot make a static reference to the non-static type
 
Similar Threads
Linked List
Question about binary tree delete method (non-API; hand-coded)
Calling method from another class
trying to call a method but cannot find symbol
Question about deleting a node from a binary tree (non-API binary tree; hand-coded)