wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Package statement and Import statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Package statement and Import statement" Watch "Package statement and Import statement" New topic
Author

Package statement and Import statement

Tusshar Shah
Greenhorn

Joined: Sep 23, 2008
Posts: 7
Hi All,

I have recently started my SCJP preparation. I am using K&B book for Java 5.

Source file declaration rules are given in 1st chapter pg no. 11.
4 and 5th point reads:

4) If the class is part of a package, the package statement must be the first line in the source code file, before any import statements that may be present.

5) If there are import statements, they must go between the package statement (if there is one) and the class declaration. If there isn't a package statement, then the import statements must be the first line in the the source code file. If there are no package or import statements, the class declaration must be the first line in the source code file.

My doubt is what is the difference between package statements and import statements. Please explain with proper example.

Thanks in advance.


Best Regards<br />Tusshar Shah
Lakuma Yalamanchili
Greenhorn

Joined: Sep 24, 2008
Posts: 26
Tusshar,

Here's an example:
  • If there is a package statement, that is the first line of your source code.


  • If there is an import statement, then it should follow your package statement and all the imports should start before a class definition.



  • If there is an import statement and no package statement, then the import statement would be your first statement of the source code.



  • If there is no package statement and there are no import statements, then obviously the class declaration would be the first line of your source code.




  • Hope this makes it clear!

    Regards,
    Lakuma
    [ October 03, 2008: Message edited by: Lakuma Yalamanchili ]
    Tusshar Shah
    Greenhorn

    Joined: Sep 23, 2008
    Posts: 7
    Hi,

    Thanks for such a quick response. I very well understood what are positioning rules for package and import statements.

    Now i want to know why do we write package and import statements and what is the difference beween the two?
    Ashish Hareet
    Ranch Hand

    Joined: Jul 14, 2001
    Posts: 375
    Tusshar,

    Here's a link from Sun's tutorials - http://java.sun.com/docs/books/tutorial/java/package/index.html

    HTH
    Ashish Hareet
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    The link Ashish provided goes into greater detail, but here's a quick example.

    A package statement "puts" the class/interface into a specific package. For example, I might use the statement package net.javatoid.tools; to put a class into my package net.javatoid.tools.

    Now suppose you are using my package (and you've installed this package where Java can find it on your system). You could identify that class by its fully qualified name everywhere you use it. For example...

    net.javatoid.tools.CalcMachine machine = new net.javatoid.tools.CalcMachine();

    An alternative is to use an import statement, which allows members of different packages to be addressed by their simple names. For example...

    import net.javatoid.tools.CalcMachine;

    Then instead of using the fully qualified name everywhere you needed it, you can identify the class by its simple name, CalcMachine. This can make your code more manageable...

    CalcMachine machine = new CalcMachine();


    "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    sscce.org
    kiruthiga arumugam
    Greenhorn

    Joined: Sep 29, 2008
    Posts: 8
    Originally posted by Tusshar Shah:
    Hi All,

    I have recently started my SCJP preparation. I am using K&B book for Java 5.

    Source file declaration rules are given in 1st chapter pg no. 11.
    4 and 5th point reads:

    4) If the class is part of a package, the package statement must be the first line in the source code file, before any import statements that may be present.

    5) If there are import statements, they must go between the package statement (if there is one) and the class declaration. If there isn't a package statement, then the import statements must be the first line in the the source code file. If there are no package or import statements, the class declaration must be the first line in the source code file.

    My doubt is what is the difference between package statements and import statements. Please explain with proper example.

    Thanks in advance.
    kiruthiga arumugam
    Greenhorn

    Joined: Sep 29, 2008
    Posts: 8
    hi

    a package should have all related this about a particular class,so the import statements are very important for extending as well as implementing classes and interfaces.if you miss them while packaging by putting the import statements above the package statement that particular class will end up in error only..so package stament should be placed on the top of all codes..
    Anoobkumar Padmanabhan
    Ranch Hand

    Joined: Aug 08, 2007
    Posts: 103
    Hi

    package statement specifies in which packeage, our class is stored.

    import statement specifies what are all packages(classes in which all packages) we are using in our application.

    For example, my class ExamClass, is stored in the package, myPack. also, it uses some arraylist for doing particular operations. then it should implement the ArrayList class. the following piece of code will demonstrate it.


    package myPack; // the class will belongs to myPack

    import java.util.ArrayList; // this class uses Arralist class
    //inside util package

    public class ExamClass{
    //definition of class
    }


    Thanks<br /> <br />Anoobkumar<br />SCJP 1.5
    Tusshar Shah
    Greenhorn

    Joined: Sep 23, 2008
    Posts: 7
    Hi All,

    First of all thanks for such a good explanation.

    Let suppose i am writing one program where i have created one class called Nano. I want to add this class to one package called Car. So i have to include package statement
    package Car;
    in the begining of the program. So package statement will add class Nano in package Car. Is that correct?

    According to Marc's explanation, if i want to use a particular class which is in particular package then i have to use it's full name if i am not importing that package. so import statement allows to directly use class name instead of giving it's full name which includes package name also. correct?

    I just want to make sure that whatever i have understodd is correct or not.
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Yes, that is correct.

    Note that directory structure is also important. To be in a package called "car," the class file should also be in a directory called "car."

    Also note that package names begin with a lowercase letter by convention.

    Fully qualified names are described in the Java Language Specification...
  • The fully qualified name of a top level class or top level interface that is declared in an unnamed package is the simple name of the class or interface.
  • The fully qualified name of a top level class or top level interface that is declared in a named package consists of the fully qualified name of the package, followed by ".", followed by the simple name of the class or interface.
  • So if the class Nano is in the package car, then the fully qualified name of the class is car.Nano. But with an import statement, you can refer to Nano by its simple name, Nano.

    (As you probably know, the java.lang package is implicitly imported, so you don't need an import statement for that. In addition, the current package is also implicitly imported.)
    [ October 14, 2008: Message edited by: marc weber ]
    Tusshar Shah
    Greenhorn

    Joined: Sep 23, 2008
    Posts: 7
    Hi All,

    Thanks for the help. I have no further doubts.

    Special thanks to Marc.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Package statement and Import statement
     
    Similar Threads
    While running any java class in eclipse then getting error.
    another Questions.
    questions from java prepare
    Import Statment
    Source File Declaration Rules