< Day Day Up > |
Recipe 3.3 Creating Java Packages3.3.1 ProblemYou want to put your code into a Java package. 3.3.2 SolutionSelect File New Package, or right-click the Package Explorer in the Java perspective and select New Package. Alternatively, you can specify the package when you create a new class. 3.3.3 DiscussionIn all but the most trivial projects, you usually put your Java code into a package. To create a new package, select a project in the Package Explorer and then select File New Package, or right-click the Package Explorer and select New Package. The New Java Package dialog appears, as shown in Figure 3-6. Enter the name of the new package, and click Finish. The new package will be added to the project in the Package Explorer. Figure 3-6. Creating a new packageWhen the new package appears in the project, you can add classes to it by right-clicking it and selecting New Class.
When you access a class in another package, you must import it using that class's fully qualified name in your code: import org.cookbook.ch03.DisplayApp;public class DisplayApp { public static void main(String[] args){DisplayApp display = new DisplayApp( );display.print( );} }
|
< Day Day Up > |