01
Nov
All classes in Java belong to some package.
All classes in Java belong to some package. Within a class definition, you can refer to another class in the same package by using the class name alone. However, a class must use the fully qualified name when referring to a class outside of its own package. Importing Packages and Classes It s inconvenient to type the fully qualified name every time you want to refer to a class in another package. By importing a package or a class into your program, you can use class names alone. Java supports three versions of the import statement: Import a single class. Use the fully qualified name of the class in the import statement. Then, the class name alone will refer to the imported class. For example: // can refer to ‘Graphics’ in following code import java.awt.Graphics; Import all the classes in a package. Use the package name, qualified by *, in the import statement. Then, all classes in the packages can be referred to by using only the class names. // can refer to all classes in java.awt using // class name alone, i. e. ‘Component’, ‘Graphics’, … import java.awt.*; Import the package. Use the package name in the import statement: import java.awt; Then to reference a class, use the last component of the package name, followed by the class name, i.e., awt.Graphics . Creating Packages You can create your own packages of classes and interfaces. This is useful for three reasons: To organize related classes and interfaces. A package gives related classes and interfaces conceptual unity. It s a good idea to group libraries of class files into packages that you can import when necessary. An example of a set of related classes is the Sprite class we developed in Chapters 3, 4, and 5. To create a package called sprite , place the following statement package sprite;
Note: If you are looking for best hosting provider to host and run your tomcat application check Astra tomcat hosting services
