wood burning stoves
The moose likes Java in General and the fly likes inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "inner classes" Watch "inner classes" New topic

inner classes

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4351

after several years of writing java code i am of the firm belief that inner classes(at least named ones) should only be used when the code inside them is highly dependent on the outer class(uses it's variables and/or methods).

here is a case in point. you don't have to read this code just refer to it.

i used some of this code for my latest project. my menu has more items in it so i decided to do like i did last time. i create a separate top-level class called Menus. well, after the usual stuff: import this and that, need a variable called such and such, i found i had a problem with the two custom actions that are inner classes of the main class. after checking them out i decided i could make them top level classes also. i know this was just an example for the tutorial, but PLEASE.

also i hate their use of braces

don't use the tutorials as an example of good style

now comes the fun part. i get to replace 200 or so lines of code in the main class with 2
private Menus menus = new Menus(this);

Visit my download page
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4351

i am sorry i was so sure of myself earlier. i have a problem. the UndoAction need a reference to the RedoAction and vice versa. i thought this would be a piece of cake. please help me. the UndoAction wants a reference to the RedoAction and vice versa.
this really screws things up. i have tried a lot of things to no avail. for example, in the constructor i just told it to set enabled true instead of false. yeah that works untill i type something then it is disabled again.
this worked fine when it was inner classes, but i need them to be top level classes so both Word andf Menus can have access to them

this undo and redo stuff doesn't impress me much anyway. undo is the same as backspace and redo you can just type the letter again anyway. if someone can't help me with this i will just delete the features
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4351

i give up i have been stuck on this all day and it is not worth it. f an UndoManager...worthless class

i can't believe they wrote a class that just does what the backspace key does...and undoes it of course

well, that's it. screw the undo and redo. i think they just added it to the tutorial to suggest it is useful

i still get to replace 200 lines of code with 2 though
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

I'll be honest that I didn't read the code, too much of it. But the undo action and redo action shouldn't be tightly coupled in real code. You should probably have two stacks of 'Actions' each action that can be done is put into the Undo stack (normal forward moving actions would empty the Redo stack). The Undo action pops from the Undo stack, does the work, and puts the Action on the Redo stack. The Redo action should pop from the Redo stack, perform the action and put to the Undo stack.

The Undo action and Redo action should take its state from the size of its particular stack (i.e. Undo stack is empty makes Undo action disabled). Each action requires two stacks but knows nothing about other Actions. It just has to be able to respond to the size of the stack.

I agree. Here's the link: http://aspose.com/file-tools
subject: inner classes
It's not a secret anymore!