To create a package is quite easy ,simply include a Package command as the first statement in a java source file.Any classes declared within that file will belong to the specified package.The package statement defines a name space in which classes are stored.If you omit the package statement ,the class names are put into the default package ,which has no name .while the default packages is fine for short ,sample program it is inadequate for real applications .Most of the time ,you will define a package for your code.
Given that packages exist and are a good mechanism for compartmentalizing diverse classes from each other, it is easy to see why all of the built-in Java classes are stored in packages. There are no core Java classes in the unnamed default package; all of the standard classes are stored in some named package. Since classes within packages must be fully qualified with their package name or names, it could become tedious to type in the long dot-separated package path name for every class you want to use. For this reason, Java includes the import statement to bring certain classes, or entire packages, into visibility. Once imported, a class can be referred to directly, using only its name. The import statement is a convenience to the programmer and is not technically needed to
write a complete Java program. If you are going to refer to a few dozen classes in your application, however, the import statement will save a lot of typing.
This is the general form of the package statement
package pkg;
eg :
In a Java source file, import statements occur immediately following the package statement (if it exists) and before any class definitions. This is the general form of the import statement:
Here, pkg1 is the name of a top-level package, and pkg2 is the name of a subordinate package inside the outer package separated by a dot (.). There is no practical limit on the depth of a package hierarchy, except that imposed by the file system. Finally, you specify either an explicit classname or a star (*), which indicates that the Java compiler should import the entire package. This code fragment shows both forms in use:
1)Create Folder Example
2)Create another directory inside Example named Mypack
3)Copy the file example.java inside mypack
4)Exit from Mypack directory and open Example directory
5)copy the file exampletest.java file inside Example directory
6)open cmd and compile all the java file
7)Run the exampletest.java file
Program :example.java
file path :C:/Java/mypack/example.java
package mypack;
public class example
{
public String test(String str)
{
return str;
}
}
Program :exampletest.java
file path :C:/Java/exampletest.java
import mypack.*;
class exampletest
{
public static void main(String args[])
{
example obj = new example();
System.out.println("Return value "+obj.test("vineeth"));
}
}
Given that packages exist and are a good mechanism for compartmentalizing diverse classes from each other, it is easy to see why all of the built-in Java classes are stored in packages. There are no core Java classes in the unnamed default package; all of the standard classes are stored in some named package. Since classes within packages must be fully qualified with their package name or names, it could become tedious to type in the long dot-separated package path name for every class you want to use. For this reason, Java includes the import statement to bring certain classes, or entire packages, into visibility. Once imported, a class can be referred to directly, using only its name. The import statement is a convenience to the programmer and is not technically needed to
write a complete Java program. If you are going to refer to a few dozen classes in your application, however, the import statement will save a lot of typing.
This is the general form of the package statement
package pkg;
eg :
package PackageName;
In a Java source file, import statements occur immediately following the package statement (if it exists) and before any class definitions. This is the general form of the import statement:
import pkg1[.pkg2].(classname|*);
Here, pkg1 is the name of a top-level package, and pkg2 is the name of a subordinate package inside the outer package separated by a dot (.). There is no practical limit on the depth of a package hierarchy, except that imposed by the file system. Finally, you specify either an explicit classname or a star (*), which indicates that the Java compiler should import the entire package. This code fragment shows both forms in use:
import java.util.Date;
import java.io.*;
How to compile and execute java package
1)Create Folder Example
2)Create another directory inside Example named Mypack
3)Copy the file example.java inside mypack
4)Exit from Mypack directory and open Example directory
5)copy the file exampletest.java file inside Example directory
6)open cmd and compile all the java file
7)Run the exampletest.java file
Program :example.java
file path :C:/Java/mypack/example.java
package mypack;
public class example
{
public String test(String str)
{
return str;
}
}
file path :C:/Java/exampletest.java
import mypack.*;
class exampletest
{
public static void main(String args[])
{
example obj = new example();
System.out.println("Return value "+obj.test("vineeth"));
}
}
Output
C:\java>java exampletest
Return value vineeth
0 comments:
Post a Comment