packages in the import statement


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: packages in the import statement

  1. #1
    kevin Guest

    packages in the import statement


    Hi guys

    where does the package list come from in the import statement, and how to
    add to that list so that I can use other classes.


  2. #2
    Kent Guest

    Re: packages in the import statement


    Kevin,

    Every Java class belongs to a package. This is specified in the .java source
    file using the package keyword:

    package com.domain;

    If no package statement exists then the class is placed in the 'default'
    package. The default package should only really be used when you're testing
    something out and you can't be bothered with package names.

    The .java and .class files (produced by compiling the .java files) must be
    in a directory structures corresponding to their package names.

    For example, the .java file above in the com.domain package must be in a
    directory .../com/domain. The directory containing the 'com' directory must
    be added to the CLASSPATH for these classes to be found.

    When you read API documentation you are told the fully qualified class name
    (which is just the package name and the class name such as com.domain.MyClass).
    To use this class in your own code make sure it is on your CLASSPATH (always
    true for core Java classes) and import it like this:

    import com.domain.MyClass;

    or import the whole package:

    import com.domain.*;

    So to use the java.util.Properties class you could write:

    package com.domain;

    import java.util.Properties;

    public class MyClass {
    public static void main(String[] args) {
    Properties props = new Properties();
    //this is OK because you have imported the class
    }
    }

    Hope that helps,
    Kent


    "kevin" <kevinber@shaw.ca> wrote:
    >
    >Hi guys
    >
    >where does the package list come from in the import statement, and how to
    >add to that list so that I can use other classes.
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center