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

Q: Why developers should not write programs that call 'sun' packages?<br />

Answer: <strong>Java</strong> Software supports into the future only classes in java.* packages, not<br />

sun.* packages. In general, API in sun.* is subject to change at any time without<br />

notice.<br />

A <strong>Java</strong> program that directly calls into sun.* packages is not guaranteed to work on<br />

all <strong>Java</strong>-compatible platforms. In fact, such a program is not guaranteed to work even<br />

in future versions on the same platform.<br />

For these reasons, there is no documentation available for the sun.* classes.<br />

Platform-independence is one of the great advantages of developing in the <strong>Java</strong><br />

programming language. Furthermore, Sun and our licensees of <strong>Java</strong> technology are<br />

committed to maintaining backward compatibility of the APIs for future versions of the<br />

<strong>Java</strong> platform. (Except for code that relies on serious bugs that we later fix.) This<br />

means that once your program is written, the class files will work in future releases.<br />

For more details, see the article Why Developers Should Not Write Programs That<br />

Call 'sun' Packages.<br />

http://java.sun.com/products/jdk/faq/faq-sun-packages.html<br />

Q: Can garbage collector remove my singleton?<br />

A usually singleton..<br />

public class Single{<br />

private static Single single;<br />

private Single {}<br />

public static Single getInstance(){<br />

if(single==null){<br />

single = new Single();<br />

}<br />

return single;<br />

}<br />

}<br />

Well,, seems good ?<br />

But classes are objects too...so do <strong>Java</strong> 2 v1.3 class garbagecollecting? Meaning my<br />

singleton could dissapear if i dont keep a refrence to it (or the class itself) somewhere<br />

?<br />

If classes is not garbagecollected, that's pretty stupid, I dont want classes taking up<br />

memory when i perhaps never will use it again....<br />

Answer: No. Classes can define objects. That is, only the dynamic part of the class<br />

defines objects. The static part exists only in one place in memory and can not be<br />

duplicated. You can call the getInstance() method from anywhere in your program.<br />

<strong>Java</strong> requires however that you tell where to find the method, in this case in the<br />

Single class.<br />

Therefore, you should use<br />

Single.getInstance()<br />

to get the instance. This is (though it looks much like it) not an execution of a method<br />

on an object, but just a method call without object. Single is only used to find out<br />

which getInstance() method should be used, and where it is.<br />

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

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

Saved successfully!

Ooh no, something went wrong!