Forums Register Login

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

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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).
The human mind is a dangerous plaything. This tiny ad is pretty safe:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 13235 times.
Similar Threads
Linked List
Question about binary tree delete method (non-API; hand-coded)
trying to call a method but cannot find symbol
Calling method from another class
Question about deleting a node from a binary tree (non-API binary tree; hand-coded)
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 15:34:07.