[java] What is deprecation?

Started by
4 comments, last by Big_Bad_Bill 22 years, 9 months ago
I''m just starting to learn the Java language and I find it very fun, but I was wondering what deprecation is. After compiling my project I get four warnings, all saying that methods I am using are deprecated. It says "size().width" "size().height" "public boolean keyDown(Event e, int key)" and "public boolean mouseDown(Event e, int x, int y)" are all deprecated. My applet runs perfectly at it''s current stage, but I just want to know how to modify my methods or what methods I should actually be using, to get the deprecation warnings to stop. Any help would be fine. Thanks.
Advertisement
In java a "deprecated" method is one that is marked as no longer for use. It means that sun wish the method to be retired. You can use it, but there is the chance that in future versions of java it won''t be there. Usually an alternative is supplied, just look in the javadoc for this.

In your case size() has been replaced by getSize() (as it is more consistently named).

"public boolean keyDown(Event e, int key)" and "public boolean mouseDown(Event e, int x, int y)" are part of the old way of handling events. The new event handling mechanism allows you to create "listeners" that listen for the events. This allows more flexability.
Look at java.awt.event.* for more info.

For example to replace you mouseDown event you could do:

addMouseListener(
new MouseAdapter() {
public void mousePressed( MouseEvent me ) {
System.out.println( me.getX() + " " + me.getY() );
}
} );

Hope that helps,
John

Little Spikey Land
Hi!

A "deprecated" method is a mehtod that has been replaced by a better method. Try to look at the APi doc for information about the replacement method. In your case, if I remember correctly, they are replaced by the methods getSize() and setSize().

Regards
Johan
Johan Karlsson
why depracated ? . why dont java just release additions to the jdk and not remove older classes (or methods? ) .

look at C++, you can have the same functionality with normal C and C++ with stl, even though redundent sometimes, but old obsolete functions are still present for compatibility..

i believe that you can still compile 16 bit functions in Win32 because of compatibility with win 3.1



{ Stating the obvious never helped any situation !! }
Well if the methods weren''t marked as deprecated we would not know we weren''t supposed to be using them.
depreciation also means that the function might be going away, and therefore make it more logical ( except in compatibility ways ) to make that one line change to use the non-depreciated version.

But don''t come crying when the function is finally actually removed. Sun has given most depreciations at least two versions before removal.

This topic is closed to new replies.

Advertisement