27.04.2013 Views

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

General <strong>Java</strong> Questions IV<br />

of it is as a way to design plug-ins.<br />

For example, let's say we have an application that monitors a network of computers.<br />

Our monitors might check for web pages, or they may check for other ports, or they<br />

may have hooks for hardware checks.<br />

The interface to our main control panel is always the same: We need some means to<br />

poll the monitor object for an answer. This is the "NetworkMonitor" interface and all<br />

network monitors will share this interface, but they may have a class heirarchy that is<br />

very different, for example, port-monitors may all fork a thread that periodically<br />

checks whereas our control panel interface just asks for the most recent answer;<br />

hardware monitors may ask for their data in real-time or over RPC and thus have no<br />

need of inheriting from Thread.<br />

Because they share the same Interface definition, the control panel application does<br />

not need to know if they are polling monitors or real-time monitors because, from the<br />

control panel's point of view, it does not matter<br />

--<br />

...<br />

P.S. by John<br />

Also interfaces make our life (I mean programmers) much easier.<br />

Imagine a big project ( a lot of programmers, many packages, thousands of files): it is<br />

impossible all the time to be aware about particular implementation of some method<br />

in specific class in another package!<br />

Much easier to define interfaces between packages as some kind of bridges or gates<br />

into another package and their functionality, with hidden (for us) implementation. We<br />

just know interface and method names. It is enough to use those methods.<br />

How it is implemented there does no matter... It is working!<br />

Q: Is a static variables value likely to be garbage collected once objects of the<br />

class are no longer pointing to it? Does it destroy itself at any point or must that be<br />

done explicitly?<br />

I have a class with a static variable I create two instances of this class.<br />

In each instance I modify the static variable. Then I destroy the objects(point them to<br />

null), then create another instance of the same class and reference the static<br />

variable.<br />

Answer: No - the static variables are effectively linked to the Class object<br />

rather than any instances; they will only be garbage collected when the<br />

Class is garbage collected. This will only happen if the ClassLoader<br />

which loaded the Class is garbage collected.<br />

--<br />

Jon Skeet<br />

http://www.pobox.com/~skeet<br />

this advice first was published on comp.lang.java.programmer<br />

Q: I find such function, but in Class class. Is it possible to use it (or something<br />

like this) in another class? Exactly I would like call: super.super.method1();<br />

Answer: No, you can only access methods of your direct ancestor.<br />

The only way to do this is by using reflection:<br />

import java.lang.reflect.*;<br />

file:///F|/350_t/350_tips/general_java-IV.htm (8 of 10) [2002-02-27 21:18:34]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!