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 I<br />

efforts to create a socket on that same port would throw an exception.<br />

Answer 2: Actually a better way is to (on launch):<br />

1) Check if the file exists. If not, create it, open it and run. Leave it open until you quit,<br />

upon which time you close it.<br />

2) If the file _does_ exist, try to delete it. If the delete fails, then someone else has it<br />

open, which means another copy of your app is running. Inform the user and quit.<br />

3) If you succeeded in deleting it, then you are the first. Now create, open and run.<br />

Doing the above prevents the problem of having the semaphore file left around when<br />

the system crashes. I implemented it recently in one of our apps, and it works like a<br />

charm.<br />

--<br />

Burt Johnson<br />

MindStorm Productions, Inc.<br />

http://www.mindstorm-inc.com<br />

Q: Can you call a class static abstract method from an abstract class or does it<br />

need to be extended and then called from its concrete class?<br />

I've been told that abstract classes do not actually have any code in them cause they<br />

are like a placeholder, so I guess you wouldn't bother calling a static method in an<br />

abstract class cause it wouldn't have any code to begin with....?<br />

Answer: You have been told wrong. Abstract classes can and do have code in them.<br />

See, for example, java.awt.Component, an abstract class with a lot of code and no<br />

abstract methods at all. If a class has any abstract method member, directly declared<br />

or inherited, it is required to be declared abstract. If not, it is the programmer's<br />

decision and should be based on whether it would make sense to have an instance of<br />

that class.<br />

Perhaps whoever told you was confusing abstract classes with interfaces, which don't<br />

contain implementation, only abstract method and constant declarations.<br />

You cannot declare a method to be both static and abstract. Abstract requires the<br />

method to be overridden before you can have a concrete class, static prevents<br />

overriding. You can have a static method in an abstract class - such a method could<br />

be called without creating an instance of the class, the only thing that is prohibited for<br />

abstract classes.<br />

And when a subclass of an abstract method has been instantiated, all the methods<br />

from the original class will keep the same code in the instance. Most of the time an<br />

abstract class will have abstract methods.<br />

However, there are several examples of abstract classes that don't have any abstract<br />

methods at all. Some examples are Component and FontMetrics from the AWT. It<br />

doesn't make sense to have just a Component that's not a specific type of<br />

component. It doesn't make sense to have a FontMetrics that doesn't measure any<br />

specific kind of Font.<br />

Also being abstract never prevents overriding, it just requires overriding in order to<br />

derive a non-abstract subclass. And if a class is a subclass of an abstract class, it<br />

only MUST override those methods declared abstract. The other methods do not<br />

require overriding.<br />

file:///F|/350_t/350_tips/general_java-I.htm (28 of 31) [2002-02-27 21:18:18]

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

Saved successfully!

Ooh no, something went wrong!