This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hello, In my posting „Declare and use interface“ Anthony Villanueva showed my a code which creates Package „math.Vector“ For source file Vector.java and imports package „math.Vector“ in Vectorpackagtest.java. But he only told me the coding not the way to create / compile the source files Vector.java and Vectorpackagetest.java. I think it is quite difficult to create packages so I did it myself for this example and want you to show the results:
1. CREATE PACKAGE: Save Source File Vector.java into package „math“ Here's a sample for creating „Vector.java“ in Package „math“.I have a "root" directory in C:\anthony\java\applications. I create a C:\anthony\java\applications\math folder and define a Vector.java in this folder with the following code:
When you compile Vector.java please name path where it is stored c:\anthony\java\applications\math otherwise compiler won’t find source file and issue error: „error: cannot read: Vector.java“. Also state root directory <c:\anthony\java\applications> and –d (package option) when compile source file with javac ... Therefore compilation statement is „javac –d\ c:\anthony\java\applications\ A .class file is created in folder „c:\anthony\java\applications\math“. DOS-statements to compile Vector.java into package „math“ > cd C:\anthony\java\applications\math > javac -d c:\anthony\java\applications Vector.java C:\anthony\java\applications\math>javac -d c:\anthony\java\applications Vector.java
2. IMPORT PACKAGE Note the package declaration at the very top. In C:\anthony\java\applications I define Vectorpackagetest.java which imports math.Vector:
Note the import command at the very top. Since it is very possible that classes may share the same name, packages are used to differentiate them, in the same sense that different file paths distinguish different files with the same name. For example, there is a java.util.Date as well as a java.sql.Date class. In the example above we now have a math.Vector class in addition to the usual java.util.Vector. DOS-Statements to compile „Vectorpackagetest.java“ > cd C:\anthony\java\applications > javac Vectorpackagetest.java C:\anthony\java\applications>javac Vectorpackagetest.java Run „Vectorpackagetest.java“: DOS-Statements to run „Vectorpackagetest.java“ > cd C:\anthony\java\applications > java Vectorpackagetest C:\anthony\java\applications>java Vectorpackagetest Result: > cd C:\anthony\java\applications > java Vectorpackagetest