File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes core java (j2se 5.0) tutorial & package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "core java (j2se 5.0) tutorial & package" Watch "core java (j2se 5.0) tutorial & package" New topic

core java (j2se 5.0) tutorial & package

Amit Sethi
Ranch Hand

Joined: May 30, 2006
Posts: 45
I have searched many places but i did'nt get core java tutorial in pdf or doc. can any body have a link to the tutorial's pdf version ?

Q. what is the main purpose of package ? Is it only for packing classes, interface, enum, annotations in directories ? (after refering tutorial)

Pleaes help me
Amit Sethi.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46334
I don't think the tutorial is available on dic or pdf format. You get a selection of download-able versions here, but they appear all to be in html. There may be some parts of the tutorial which are only available in the on-line version.

If you have a large application, then you put files which are related into packages by writing as the very first statement of your file, before the import statements. You usually keep package names short, and use lower case only.

You can have packages inside packages: the Math class starts with

If you are planning to let your application out into the big wide world, it is usual to start your package name with your website URL, without the www, written backwards. That is how you get pacakge names starting in com.sun.

As I said, the use of a package declaration means you can keep classes which are related to one another together. You can have classes with the same name in different packages. You can use classes from your other packages by using an import statement in the usual fashion. You can compile every class in a package and zip the whole lot up as a .jar and import the jar into a build path for a different project.

Beware: If you are using packages and compiling from command prompt you have to use a certain sequence of instructions:-
  • Don't put any of the .java files into a particular folder; keep them all in the same folder regardless.
  • Compile the files in the deepest packages first. That means the files "farthest" from the main method.
  • Use the following sort of instruction to compile: C:\MyJavaProgramsFolder> javac -d .
  • -d means to put the .class files into a directory, and the . means to start with the "." directory, ie the current directory you happen to be in.
  • If you get any compiler errors like "Cannot find symbol: class Foo," compile Foo on its own with -d . included
  • Compile the class with the main method last. Use the same sort of instruction as above with "-d ."
  • If your main method is in package bar in class FooTest, start the app off with the instruction java bar.FooTest
  • Campbell Ritchie

    Joined: Oct 13, 2005
    Posts: 46334
    Further to my previous posting.

    If you start off with all your .java files in one folder, you will find the Java compiler creates directories corresponding in name to the packages and subpackages. If you use IDEs, they all have different approaches to where they put the .java and .class files.

    And if you have two questions, you would have done better to start two threads. Poeple reading the title of this thread will think it is only about how to get the tutorial.

    [ June 14, 2006: Message edited by: Campbell Ritchie ]
    I agree. Here's the link:
    subject: core java (j2se 5.0) tutorial & package
    It's not a secret anymore!