Importing Java Packages

by Gabriela C. Perez.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on java  

You are here: Categories » Computers and technology » JAVA

To use a package within a Java application or applet, we need to import it. We do this by means of the import keyword. So, for example, if we wish to include the I/O package, which is called java.io, we would have the following statement at the top of our code (before we define any classes):

import java.io.*;

Note how we have appended an extra decimal point and star to the end of the package name. This means that it will include all of the classes within the package (i.e., the asterisk is used as a wildcard).

Another example of this would be if we wished to include the utility package, which is called java.util. This would be done with the following statement:

import java.util.*;

Again, note the use of the asterisk to include all the classes from the package. However, if we only wished to include a single class from the package, we could do this too.

Within the utility package, there is an ArrayList class. If we simply wish to use the ArrayList class from the utility package and no others, we could import just the ArrayList class using the following statement at the top of our code.

import java.util.ArrayList;

Of course, if we used the asterisk, the ArrayList package would be included automatically. So once we do this, we could then create a reference to an ArrayList object within a class or method using the following statement:

ArrayList myArrayList;

Also, it is good to know that it is possible to access the ArrayList class (or any other class out of a package) by using its fully qualified name. For example, without any import statements, we could create the myArrayList object as we did before with the following line of code.

java.util.ArrayList myArrayList;
As we mentioned in the introduction, packages provide namespace management, so it is therefore possible that two packages could both have a class with the same name in it. Obviously, this could cause problems if both the packages were imported, so in this case it would make sense to use the fully qualified package name:

package1.MyClass firstReference;
package2.MyClass secondReference;
Leave a comment or ask a question
Total comments: 0

JAVA Disclaimer

  • The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
SELECTION STATEMENTS IN JAVA - Like other programming languages java offers the control statements to control the execution of a program. The control statements in java are the selection statements, loop constructs and the jump (more...)
JAVA and its Advantages - In today's highly competitive world, JAVA has become one of the most secure technologies for website and software development. Prime benefits of JAVA are platform independency and easy availability (more...)
Java Tactics: Earn A Fortune With Java - Get the right Knowledge Get the right Papers Get the right Direction Get the Money Hi, my name's (more...)
Introduction to Object Oriented Programming (OOP) - The transition from a procedural programming (non-OOP) language to an object-oriented programming language is a large step for many programmers. It is true that both methods of programming can ulti (more...)
Operator Precedence in programming languages - Operator precedence deciphers the order in which calculations in an expression occur. Looking at the calculation example 3 + 4 * 6, the answer could be calculated by adding 3 and 4, which gives (more...)
Arithmetic Assignment Operators - The following assignment operators are similar to the increment and decrement operators that we have just seen. They are used so that you do not need to enter the source variable twice when ass (more...)
Bitwise Operators - The following table shows the standard bitwise operators in Java and a description of them. (more...)
Invocation Chaining - Invocation chaining means that you are not limited to merely accessing one class/object member in a given statement with the . operator but may continue to access further members in a given stateme (more...)
Regular Expressions in Java - A regular expression is a code that is used to match a pattern in a given string and is new to Java 1.4. Regular expressions are made up of normal characters and metacharacters. Normal characters a (more...)
Character Escape Sequences - Character escape sequences allow for a character to be interpreted differently than its literal value. Character escape sequences are defined using the backslash (\) character, followed by th (more...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.