Packages are containers for classes.We can define classes inside a package that are not accessible by code outside that package.A package can be defined by including a package command as the first statement in the Java source file.Any classes declared within that file will belong to the specified package.The package statement defines a namespace in which classes are stored.If we omit the package statement,the class names are put into the default package,which has no name.
General Form:
package package_name ;
Example: package MyPackage ;
which creates a package called MyPackage.Java uses the file system directories to store packages ,that is the class file for any classes that declared to be a part of MyPackage and must be stored in a directory called MyPackage.The directory name must match the package name exactly.More than one file can include the same package statement.We can create a hierarchy of packages.To do so simply separate each package name from the one above it by use of a period.The general form of a multilevel package statement is ;
package pkg1.[pkg2.[pkg3]] ;
A package hierarchy must be referred in the file system of the Java Development System,
package java.awt.image ;
needs to be stored in the java\awt\image
Example program:
package MyPackage;
class Balance
{
String name;
double bal;
Balance (String n,double b)
{
name = n;
bal = b;
show( );
}
void show( )
{
if(bal > 0)
{
System.out.println("Name = "+name+" balance = "+bal);
}
else
{
System.out.println("Name = "+name+" balance = ----");
}
}
}
class AccountBalance
{
public static void main(String args[ ])
{
balance current[ ] = new Balance[3];
current[0] = new Balance("John",123.25);
current[1] = new Balance("Jack",155.99);
current[2] = new Balance("Tom",160.00);
}
}
OUTPUT
Name = John Balance = 123.25
Name = Jack Balance = 155.99
Name = Tom Balance = 160.00
General Form:
package package_name ;
Example: package MyPackage ;
which creates a package called MyPackage.Java uses the file system directories to store packages ,that is the class file for any classes that declared to be a part of MyPackage and must be stored in a directory called MyPackage.The directory name must match the package name exactly.More than one file can include the same package statement.We can create a hierarchy of packages.To do so simply separate each package name from the one above it by use of a period.The general form of a multilevel package statement is ;
package pkg1.[pkg2.[pkg3]] ;
A package hierarchy must be referred in the file system of the Java Development System,
package java.awt.image ;
needs to be stored in the java\awt\image
Example program:
package MyPackage;
class Balance
{
String name;
double bal;
Balance (String n,double b)
{
name = n;
bal = b;
show( );
}
void show( )
{
if(bal > 0)
{
System.out.println("Name = "+name+" balance = "+bal);
}
else
{
System.out.println("Name = "+name+" balance = ----");
}
}
}
class AccountBalance
{
public static void main(String args[ ])
{
balance current[ ] = new Balance[3];
current[0] = new Balance("John",123.25);
current[1] = new Balance("Jack",155.99);
current[2] = new Balance("Tom",160.00);
}
}
OUTPUT
Name = John Balance = 123.25
Name = Jack Balance = 155.99
Name = Tom Balance = 160.00
No comments:
Post a Comment