Understanding Packages and Interfaces in Java
page 4 of 11
by Debjani Mallick
Feedback
Average Rating: 
Views (Total / Last 10 Days): 59753/ 149

Setting up the classpath

When we write a java program file having extension .java, it is kept under a particular directory. After compilation, the class file is created in the same directory. To execute the program we have to specify the class filename only. If a package is included then the name of the package must be a directory and after compiling the java file, the class files are stored under that package directory. To run a class of a particular package the following commands must be used.

Listing 3

java packagename.classname

However, this command must be given from the directory in which the package directory is present. For our Firstappplication example, if we are placing the Example package under C directory then we can set the classpath as:

Listing 4

set classpath = .;C:\;

The above statement sets the classpath to point at two places: .(dot) and C directory. Dot is used as an alias to represent the current directory. Semicolon is used to separate the directory location in case class files are placed at many locations.

As mentioned earlier, if we have placed our java file inside Example directory (which is placed inside C directory), for compiling we need to go to the Example directory and write the command as shown below.

Listing 5

C:\Example\javac Firstapplication.java

If we try to run the same by using java Firstapplication, then we get an error because the Firstapplication program is now a part of the package Example. For running the application we need to specify the fully qualified class name. Let us take another example; we have a package called Hello containing the file HelloWorld.java within Example package. Whenever we want to reference the HelloWorld class, we have to use Example.Hello.HelloWorld as its fully classified class name. For using the classes stored in a package, either we can declare the fully classified class name as shown:

Listing 6

//for first package 
Example.Firstapplication firstApplication = newExample.Firstapplication();
//for the subpackage 
Example.Hello.HelloWorld helloWorld = new Example.Hello.HelloWorld(); 

or we can use the import statement:

Listing 7

import.Example.*; //import all the public classes inside Example package
import.Example.Hello.*; //import all the public classes inside Hello package
import.Example.Firstapplication; //import only Firstapplication inside Example

By using the first import statement we can use the public classes present inside Example package but not the classes of Hello package.


View Entire Article

User Comments

Title: air max shoes r Five, please,��   
Name: anciag
Date: 2012-12-05 2:43:16 AM
Comment:
r Five, please,�� said the loud-speaker
Number Five was Roger Clint��s mount with the long white stockings. ��Do you [url=http://www.airmax2013cheap.com/nike-air-max-2011-c-5.html]air max 2011[/url] know what he calls it?�� Bee said. ��Operation Stockings.��
��It��s very ugly,�� Brat said. ��Looks as if he had walked through [url=http://www.airmax2013cheap.com/]nike new air max shoes[/url] air max 2013 a trough of whitewash.��
��He can jump, though.��
He could certainly jump, but he had phobia about water.
��Poor Roger,�� laughed Bee, watching Stockings refuse the water. ��He has been jumping him backwards and forwards across the duck pond at home in the hope of curing him, and now he does this!��
Stockings continued to refuse, and Clint had to take him out, in [url=http://www.airmax2013cheap.com/]new air max shoes[/url] a burst of sympathetic applause.
Numbers Six and Seven had one fault each.
Number Eight was Simon on Timber.
The black horse came into the ring exactly as he had come out of his box on the day Brat first saw him, pleased with himself and ready fo
Title: wholesale cheap ravens jerseys   
Name: ARMANI
Date: 2012-06-03 10:53:21 PM
Comment:
http://jerseymember.com/Baltimore-Ravens-Jerseys-s617.html
Title: 2012 NFL jerseys   
Name: NIKE NFL jerseys
Date: 2012-05-20 11:28:31 PM
Comment:
[/pre]Cheap NFL,NBA,MLB,NHL
[url=http://www.jersey2shop.com/]Jerseys From China[/url]
[url=http://www.jersey2shop.com/]2012 nike nfl Jerseys[/url]
[url=http://www.jersey2shop.com/]cheap China Jerseys[/url]
[url=http://www.jersey2shop.com/]Sports Jerseys China[/url]
[url=http://www.jersey2shop.com/NFL-Jerseys-c68/]NFL Jerseys China[/url]
[url=http://www.jersey2shop.com/NBA-Jerseys-c77/]NBA Jerseys China[/url]
NHL Jerseys China
[url=http://www.jersey2shop.com/MLB-Jerseys-c94/]MLB Jerseys China[/url]NFL jerseys For Sale online.All Our Jerseys Are Sewn On and Directly From Chinese Jerseys Factory
[/pre]
[pre]We Are Professional China jerseys Wholesaler
[url=http://www.cheapjersey2store.com/]Wholesale cheap jerseys[/url]Cheap mlb jerseys
[url= http://www.cheapjersey2store.com/]2012 mlb all atar jerseys[/url]
[url= http://www.cheapjersey2store.com/ [/url]Cheap China Wholesael[/url]
[url= http://www.cheapjersey2store.com/]Wholesale jerseys From China[/url]
[url=http://www.cheapjersey2store.com/]2012 nike nfl Jerseys[/url]Free Shipping,Cheap Price,7 Days Deliver
[/pre]
[/pre]
We are professional jerseys manufacturer from china,wholesal
sports [url= http://www.cheapjersey2store.com/]Jerseys From China[/url]
[url=http://www.cheapjersey2store.com/NFL-Jerseys-c68]NFL jerseys China[/url]
[url=http://www.cheapjersey2store.com/NHL-Jerseys-c96/]NHL Jerseys China[/url]
[url=http://www.cheapjersey2store.com/NBA-Jerseys-c77/]NBA Jerseys China[/url]
[url=http://www.cheapjersey2store.com/MLB-Jerseys-c94/]MLB Jerseys China[/url]
[url= http://www.cheapjersey2store.com/]China Jerseys[/url],Free Shipping
[/pre]
[/pre]
We are professional jerseys manufacturer from china,wholesal
sports [url= http://www.jerseycaptain.com/]cheap jerseys sale online [/url]
[url= http://www.jerseycaptain.com/]2012 nike nfl Jerseys[/url]
[url=http://www.jerseycaptain.com/NFL-Jerseys-c68]cheap NFL jerseys China[/url]
[url=http://www.jerseycaptain.com/NHL-Jerseys-c96/]NHL Jerseys C
Title: Real time example   
Name: Axe
Date: 2012-04-10 10:19:44 PM
Comment:
Any real time example for package which is used in daily life
Title: creating a package   
Name: golnaz
Date: 2012-01-23 4:01:45 AM
Comment:
Thanks a lot, was very useful
Title: Packages and interfaces   
Name: Lesedi
Date: 2011-02-25 5:55:35 AM
Comment:
post more details
Title: packages and interfaces in java   
Name: sinha timir krishna murari
Date: 2010-11-14 6:59:20 AM
Comment:
explanation of the concept in full detail with the definition of the topic
Title: drawback of interface   
Name: neeraj singh
Date: 2010-09-16 4:17:34 PM
Comment:
post in detail moreeeeeeeeeeee
Title: creating package   
Name: Nitesh
Date: 2010-04-20 12:53:18 AM
Comment:
gives alot of confidence to person
Title: thakur   
Name: Nitesh
Date: 2010-04-20 12:49:47 AM
Comment:
give more information
Title: package   
Name: puja
Date: 2010-01-27 11:05:13 AM
Comment:
can give more information about advantages of packages.
Title: Mistake   
Name: Gyandeep
Date: 2009-11-19 5:16:23 AM
Comment:
Ma'am, you have made a slight mistage here. The refernce variable you have used d takes r to its account and prints r.area(). It should be d.area. In the main().

Thanks and cheers!
Title: Interfaces and packages   
Name: Tamkeen
Date: 2009-07-07 2:55:10 AM
Comment:
Very precise and easily understandable language.
Title: package   
Name: Duraipandi
Date: 2008-10-14 7:34:39 AM
Comment:
It is very nice.here syntax and examples are each and every thing fine.
Title: very nice elaboration   
Name: Regalla Naresh Reddy
Date: 2008-06-10 3:36:05 AM
Comment:
Its vey nice.If you include syntax and an exmaple on syntax looks very nice.So try it
Title: doubt   
Name: raghavendra
Date: 2008-03-15 3:01:11 AM
Comment:
i have created a package named account and i created a new folder name test inside test i written a java program to access the package account.. its saying package account not found.. kindly help me.. if any..
Title: very nice   
Name: antony
Date: 2008-01-22 6:14:33 AM
Comment:
its very very useful for beginers.so thanks for you.and also i want more in swing and awt etc
Title: Nice   
Name: Ed
Date: 2007-08-12 10:34:37 PM
Comment:
NICE ARTICLE
Title: Good work   
Name: Rick
Date: 2007-08-10 10:50:14 PM
Comment:
It is a good piece of work.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-18 11:33:40 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search