Link to home
Start Free TrialLog in
Avatar of luinzi
luinzi

asked on

What is "Recompile with depreciation"?

Using JDK.1.1.7 on my PC I try to compile a series of classes called calculator.java. When compiling these classes The compiler keeps giving me a "Warning", which goes as follows:

 calculator.java uses a deprecated API. recompile with  "-depreciation" for details.

I don't know what that means. What do I do? Do I need a newer version of JDK, or do I need to add some extra arguments into the command line? Either way I feel like a complete idiot and would like some help!
Avatar of figueira
figueira

You need to compile with the "-deprecation" command line option for the javac; it will indicate you the lines of the files that contain invocations to "deprecated" methods (or methods that can be removed in future versions, so you shouldn't use them).
Avatar of luinzi

ASKER

Sorry figueira, that hasn't really told me anything more than what the compiler said. You can have 60 points if you could explain in a little more detail what I would need to type into the command line and what is happening with the methods. Thanks.....
Well, let's go a little further. Most of the "what to do when I find a deprecation message?" can be found in the URL
http://java.sun.com/products/jdk/1.1/docs/guide/awt/HowToUpgrade.html
because most of the changes were made in the AWT. I guess an example will be better.

Supposing I'm trying to compile the following class:

public class MyApplet extends Applet {
   private Label l;
   public void init() {
      this.setLayout(null);
      Button b = new Button("Show");
      b.reshape(10, 10, 50, 20);
      add(b);
      b = new Button("Hide");
      b.reshape(100, 10, 50, 20);
      add(b);
      l = new Label("Test");
      l.reshape(10, 50, 100, 20);
      add(l);
   }

   public boolean handleEvent(Event evt, Object obj) {
      if (evt.target instanceof Button) {
         String name = (String) evt.arg;
         if (name.equals("Show")) {
            l.show();
         } else {
            l.hide();
         }
         return true;
      }
      return false;
   }
}

Compiling it with the -deprecation flag in the javac, I'll get the following messages:

-------
MyApplet.java:9: Note: The method void reshape(int, int, int, int) in class java
.awt.Component has been deprecated.
      b.reshape(10, 10, 50, 20);
               ^
MyApplet.java:12: Note: The method void reshape(int, int, int, int) in class jav
a.awt.Component has been deprecated.
      b.reshape(100, 10, 50, 20);
               ^
MyApplet.java:15: Note: The method void reshape(int, int, int, int) in class jav
a.awt.Component has been deprecated.
      l.reshape(10, 50, 100, 20);
               ^
MyApplet.java:23: Note: The method void show() in class java.awt.Component has b
een deprecated.
            l.show();
                  ^
MyApplet.java:25: Note: The method void hide() in class java.awt.Component has b
een deprecated.
            l.hide();
                  ^
Note: MyApplet.java uses a deprecated API.  Please consult the documentation for
 a better alternative.
-------
Looking for the specification for the method in the class java.awt.Component (in the API, which can be found at http://www.javasoft.com/products/jdk/1.1/docs/api/packages.html, following the links to java.awt package, and then to the Component class), you can see the following comment in the reshape(int,int,int,int) method:
------
Note: reshape() is deprecated. As of JDK version 1.1, replaced by setBounds(int, int, int, int).
------
so, if I replace the call to reshape to an equivalent call to setBounds, this problem will be solved. Most of the deprecated methods have similar notes when you look in the documentation.

correcting all problems found, let's try to compile the class again:
------
public class MyApplet extends Applet {
   private Label l;
   public void init() {
      this.setLayout(null);
      Button b = new Button("Show");
      b.setBounds(10, 10, 50, 20);
      add(b);
      b = new Button("Hide");
      b.setBounds(100, 10, 50, 20);
      add(b);
      l = new Label("Test");
      l.setBounds(10, 50, 100, 20);
      add(l);
   }

   public boolean handleEvent(Event evt, Object obj) {
      if (evt.target instanceof Button) {
         String name = (String) evt.arg;
         if (name.equals("Show")) {
            l.setVisible(true);
         } else {
            l.setVisible(false);
         }
         return true;
      }
      return false;
   }
}
------
Depending on your compiler, the compilation at this time will succeed, or fail due to the overriding of the handleEvent method. In the latter case, you should change the class design to conform with the new event model introduced in JDK 1.1. Those changes are documented in the first URL I gave you.

A final version of the class could be as follows:
------
public class MyApplet extends Applet implements ActionListener {
   private Label l;
   public void init() {
      this.setLayout(null);
      Button b = new Button("Show");
      b.setBounds(10, 10, 50, 20);
      add(b);
      b.addActionListener(this);
      b = new Button("Hide");
      b.setBounds(100, 10, 50, 20);
      add(b);
      b.addActionListener(this);
      l = new Label("Test");
      l.setBounds(10, 50, 100, 20);
      add(l);
   }

   public void actionPerformed(ActionEvent evt) {
      String name = evt.getActionCommand();
      if (name.equals("Show")) {
         l.setVisible(true);
      } else {
         l.setVisible(false);
      }
   }

}
------
is it ok now?
Just a quick hint (although I think figueira has answered this one pretty well) that might help a bit:

You said "depreciation" - in fact, it is "deprecated", which (kind of) means "made obsolete". Some of the methods you use have been replaced. Therefore, you theoretically wouldn't need a newer JDK, but an older one. *g*

That wouldn't make sense, of course. You can easily ignore those warnings for the moment, but your code might not "compile" any more in even newer JDKs. It's better (style and such) to adjust your code as described above.

Good luck!
Avatar of luinzi

ASKER

Thanks people......That was most helpful figueira. I would like to try and award you 70 points for that......I,m fairly new to this so if you don't get the points (as I have probably done something wrong!) please email me at luinz@hotmail.com. Once again ....Thanks
ASKER CERTIFIED SOLUTION
Avatar of figueira
figueira

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of luinzi

ASKER

THANK YOU..........