aspose file tools*
The moose likes Beginning Java and the fly likes save on close problem? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "save on close problem?" Watch "save on close problem?" New topic
Author

save on close problem?

Berdy Ahern
Greenhorn

Joined: Mar 12, 2005
Posts: 2
Hi I wish to save a file on close but it wont allow me to refrence any objects as it is static where I'm trying to set it up. My main class "TabDemo" has implenents actionListener but I can't also put implements WindowListener can I?
Please can you tell me how I might get this WIndowListener to work?
Here is a bit of the code:

The errors it gives me are like this:
C:\TabDemo.java:326: non-static variable this cannot be referenced from a static context
frame.addWindowListener(this);
^
C:\TabDemo.java:326: addWindowListener(java.awt.event.WindowListener) in java.awt.Window cannot be applied to (TabDemo)
frame.addWindowListener(this);
^
2 errors
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
When you're running inside a static method (such as main), there is no such thing as "this" as the method belongs to the class itself and not to a particular instance of the class.


James Carman, President<br />Carman Consulting, Inc.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Berdy Ahern:
My main class "TabDemo" has implenents actionListener but I can't also put implements WindowListener can I?

Yes actually you can. A class can implement multiple interfaces but can only extend one class.

As James mentions above, you cannot use the keyword "this" in a static method since there is no "current object". If I understand what you are doing, you should change

to

This will also mean that TabDemo needs to implement WindowListener which, as I said before, is not a problem even if it already implements ActionListener or any other interface.

HTH

Layne


Java API Documentation
The Java Tutorial
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: save on close problem?