aspose file tools*
The moose likes Beginning Java and the fly likes get variable info from subclass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "get variable info from subclass" Watch "get variable info from subclass" New topic
Author

get variable info from subclass

Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 74
here is my toString method that is in my linkedStackClass and you'll notice where it says size, well size will be entered in the gui portion of my code but how do I get my toString to see what size is equal to?

Alex Hurtt
Ranch Hand

Joined: Oct 26, 2010
Posts: 98
I am going to go out on a limb and assume this is related to your other thread from earlier today. This is one of those instances where recursion can be your friend. I whipped up the following example using your LinkedStackClass code in your other thread. (It's runnable but I had to slightly alter it to account for your other classes it references but which I do not have so it would compile. It no longer implements any interface and the StackUnderrunExceptions are now just plain Exception).



Notice I renamed your toString() to printStack(). Since you only get one shot at overriding toString(), is this really what you wanted to use it for?
As for how to get your toString() method to see the value of the size, well...the size variable would have to be an instance variable visible to the toString() method since you haven't declared any size variable local to the method.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 74
Yup that is correct. Well I might as well put the rest of my code out there. This is my DropOutStack Class maybe it would be easier to override the toString in here? I guess I'm kinda confused about some of the code you added and how it works like this part. return stackTop == null ? "" : stackTop.printStack();
Alex Hurtt
Ranch Hand

Joined: Oct 26, 2010
Posts: 98
Dustin Schreader wrote:I guess I'm kinda confused about some of the code you added and how it works like this part. return stackTop == null ? "" : stackTop.printStack();


In more verbose code that would read:



If you examine your StackNode class you will see I added the method printStack() to it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: get variable info from subclass