-
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.
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks