12.07.2015 Views

OsmWv

OsmWv

OsmWv

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

You have to know the full name*of the class you want to use inyour code.ArrayList is not the full name ofArrayList, just as 'Kathy'isn't a full name (unless it's like Madonna or Cher, but wewon't go there). The full name ofArrayList is actually:ORIMPORTPut an import statement at the top of your source code file:import java.util.ArrayList;public class MyClass {... }-java.util.ArrayList~~/ '\patka~e l'lc11l1ee.\ass ~"'cYou have to tell Java which ArrayList youwant to use. You have two options:o TYPEType the full name everywhere in your code. Each timeget to know the Java API:tberet&rH?Dumo ~uest19nsQ.: Why does there have tobe a full name? Isthat the onlypurpose of a package?A: Packagesare importantfor three main reasons. First, theyhelp the overall organization of aproject or library. Rather than justhaving one horrendously largepile of classes, they're all groupedinto packages for specific kindsof functionality (like GUI,or datastructures, or database stuff, etc.)Second, packages give you a namescoping,to help prevent collisionsifyou and 12 other programmersin your company all decide tomake a classwith the same name.If you have a class named Set andsomeone else (including the JavaAPI) has a class named Set,youneed some way to tell the JVMwhich Set class you're trying to use.Third, packages provide a level ofsecurity, because you can restrictthe code you write so that onlyother classesin the same packagecan accessit. You'll learn all aboutthat in chapter 16.you use it. Anywhere you use it. n .~: OK, back to the name. .. collision thing. Howdoes a fullWhen you declare and/or instantlate It:name really help? What's tojava.util.ArrayList list = new java.util.ArrayList0 ; prevent two people from giving aclass the same package name?When you use it as an argument type:public void go(java.util .Arraytist list) { }When you use it as a return type:public java.util.Arraytist fooO { .. . }A:Java has a naming conventionthat usually prevents this fromhappening, as long as developersadhere to it. We'll get into that inmore detail in chapter 16."Unless the class is in the java.lang package.you are here. 155

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

Saved successfully!

Ooh no, something went wrong!