ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

javatricks

Updated on August 5, 2012

Twist your Mind

Source

Package

Package is a logical container which contains classes,interfaces and subpackages.

Packages are used to provide Unique Name Space to their members as well as to define a new Scope.

Name Space Providers are:

1.Technology Provider

2.Application Provider

3.Third Party Provider

In Software Development classes of multiple parties and vendors are used hence there must be a mechanism to uniquely identify classes of each vendors this problem is resolved by packages,when classes or libraries are to be distributed .they are contained in packages.

to certain that packages of different vendors don't have same name ...

following conventions are used in naming convention packages.

→DomainName.ProjectName.PackageName

examples:-following are same packages of open source frameworks:-


Framework
Domain
Package
spring
www.springframwork.org
org.springframwork.beans
struts2
www.struts2.apache.org
org.apache.struts2.interceptors
 
 
 

Following steps are required to associate a class to a package:---

1. package keyword is used in the class definition to specify the package of the class.

Note:♣ package keyword when used must be the first statement of a class definition.

ex:

package pkgName;

class A

{

public static void main(String args[])

{

System.out.println("raushan get up");

}

}

2. source file and the generated class file are stored in a folder of a same name as the package.

→When classes are associated to packages then they are always refereed using there full Name.

ex: PackageName.className

How to Use Package reference in another class:

package p1;
public class A
{
public static void main(String args[])
{
System.out.println("referencing B of p2");
p2.B x=new p2.B();
System.out.println("Invoking display() of B....");
x.display();
}
}

In an Application Two types of classes are referred

  1. Library Classes
  2. User Defined Classes
  • Location of Library classes is fixed and known
  • Location of user defined classes are not fixed and not known

♦ ClassPath Environment Variable is used by application developer to provide their location of their classes to the compiler and the JRE.


Difference Between Path and ClassPath

Path
ClassPath
 
Path Environment Variable is used by the O/S to locate the specified file.
ClassPath Environment Variable is used by java compiler and class Loader to locate Class files.
 
from java point of view path Environment is used to locate java source file
Classpath Environment variable is used to locate classes.
 
 
 
 
Note:default value of Classpath Environment variable is current folder.

ClassPath Environment variable can be set permanently or can be specified for the compilation or execution of specific class.

Syntax:-of compiling a java classes :-

java -cp FolderToBeSearched NameOfPathOfJavaFile↑

Syntax:-of executing a java classes

java -cp FolderToBeSearched NameOfClass↑

Note:↑ is used for Enter key of keyboard.

Question:Commands to compile A.java from following location if we have situation like this.

E:\code\p1\A.java

D:\p2\B.java

Commands to compile A.java From following location:

1.E:\code\p1>javac -cp D:\ A.java↑

2.D:\>javac E:\code\p1\A.java↑

3.C:\>javac -cp D:\;E:\code\p1\A.java↑

Commands to execute the code :-----

1.E:\code\p1>java -cp D:\; E:\code\ p1.A↑ or java -cp ..;D:\; p1.A↑

2.D:>java -cp E:\code\;. p1.A↑

3.C:>java -cp D:\; E:\code\ p1.A↑

Twist your Mind

Question for you how to use packages :Please answer as a comment

Scenario:--

E:\code\p1\A.java

D:\p2\B.java

C:\p3\C.java

here A.java has dependency on B.java and C.java

so how to compile and run(execute) from:

1.E:\code\p1>

2.D:\>

3.C:\

4.F:\

4 stars from 1 rating of Packages

Which one is best language ?

See results

Comments

    0 of 8192 characters used
    Post Comment

    • gauri1234 profile image

      gauri1234 4 years ago

      this is useful..thanks

    Click to Rate This Article