learn more...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; package1.MyClass firstReference; package2.MyClass secondReference; |
||||||
Disclaimer
1) E-articles is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringement, please read the terms of service and contact us to investigate the problem.
2) E-articles is not responsible for inaccuracies, falsehoods, or any other types of misinformation this article may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here. link to this article |