Packages in Java: A.Hemlathadhevi
Packages in Java: A.Hemlathadhevi
A.Hemlathadhevi
PACKAGES
● In java there are already many predefined packages that we use while
programming.
● One of the most useful feature of java is that we can define our own packages
PACKAGES Cont...
● Types of package:
●This statement should be used in the beginning of the program to include that
program in that particular package.
●The -d switch specifies the destination where to put the generated class file
●If you want to keep the package within the same directory, you can use . (dot).
How to access package from another package?
There are three ways to access the package from outside the package.
1.import package.*;
2.import package.classname;
Using packagename.*
●If you use package.* then all the classes and interfaces of this package will be
accessible but not subpackages.
●The import keyword is used to make the classes and interface of another package
accessible to the current package.
Using packagename.classname
●If you import package.classname then only declared class of this package will be
accessible.
●If you use fully qualified name then only declared class of this package will be
accessible.
●Now there is no need to import. But you need to use fully qualified name every
time when you are accessing the class or interface.
●It is generally used when two packages have same class name e.g. java.util and
java.sql packages contain Date class.
Note: If you import a package, all the classes and interface of that package will be
imported excluding the classes and interfaces of the subpackages. Hence, you need to
import the subpackage as well.
Sequence of the program must be package then import then class.
Packaging up multiple classes
Package with Multiple Public Classes
●A Java source file can have only one class declared as public, we cannot put two or
more public classes together in a.java file. This is because of the restriction that the
file name should be same as the name of the public class with
.javaextension.
●If we want to multiple classes under consideration are to be declared as public, we
have to store them in separate source files and attach the package statement as
the first statement in those source files.
//Save as A.java //Save as B.java
package javapoint; package java
Public class B{} public class a{}
CLASSPATH
●It is a environmental variable, which contains the path for the default-working
directory (.).
●The specific location that java compiler will consider, as the root of any package
hierarchy is, controlled by Classpath
●The package name is closely associated with the directory structure used to store the
classes. The classes (and other entities) belonging to a specific package are stored
together in the same directory.
●There is a scenario, I want to put the class file of A.java source file in classes folder of
c: drive. For example:
//save as Simple.java package mypack;
public class Simple
{
public static void main(String args[])
{
System.out.println("Welcome to package");
}
}
To Compile:
e:\sources> javac -d c:\classes Simple.java
To Run:
To run this program from e:\source directory, you need to set classpath of the directory
where the class file resides.
e:\sources> set classpath=c:\classes;
e:\sources> java mypack.Simple
Another way to run this program by -classpath switch of
java:
● The -classpath switch can be used with javac and java tool.
● To run this program from e:\source directory, you can use -classpath switch of
java that tells where to look for class file. For example:
■ e:\sources> java -classpath c:\classes mypack.Simple
The Date classes encapsulate information about a specific date and time.
It provides the following constructors.
Date()
Date(long msec)
Here, the first form returns an object that represent the current date and
time.
The second form returns an object that represents the date and time msec
in milliseconds
Method Description
This class defines several integer constants that are used when you get or set
components of the calendar.
AM ,AM_PM ,APRIL,AUGUST ,DATE ,DAY_OF_MONTH
,DAY_OF_WEEK_IN_M,DAY_OF_WEEK ,DAY_OF_YEAR ,DECEMBER
,DST_OFFSET ,ERA,FEBRUARY, FIELD_COUNT, FRIDAY,HOUR
,HOUR_OF_DAY ,JANUARY,JULY, JUNE, MARCH,MAY, MILLISECOND,
MINUTE
import java.util.Calendar;
public class Cal1
{
public static void main(String[] args)
{
Calendar cal = Calendar.getInstance();
System.out.println("DATE is : " + cal.get(cal.DATE));
System.out.println("YEAR is : " + cal.get(cal.YEAR));
System.out.println("MONTH is : " + cal.get(cal.MONTH));
System.out.println("DAY OF WEEK is : " + cal.get(cal.DAY_OF_WEEK));
System.out.println("WEEK OF MONTH is : " +
cal.get(cal.WEEK_OF_MONTH));
System.out.println("DAY OF YEAR is : " + cal.get(cal.DAY_OF_YEAR));
System.out.println("DAY OF MONTH is : " + cal.get(cal.DAY_OF_MONTH));
System.out.println("WEEK OF YEAR is : " + cal.get(cal.WEEK_OF_YEAR));
System.out.println("HOUR is : " + cal.get(cal.HOUR));
System.out.println("MINUTE is : " + cal.get(cal.MINUTE));
System.out.println("SECOND is : " + cal.get(cal.SECOND));
Output :
Date is : Fri May 25 00:21:14 IST 2012
Milliseconds since January 1, 1970, 00:00:00 GMT :1337885474477T
Time after 10 second Thu Jan 01 05:30:00 IST 1970
Time after 10 second Thu Jan 01 05:30:10 IST 1970
Thu Jan 01 05:30:10 IST 1970