package problem

1 reply [Last post]
amanhoney
User offline. Last seen 27 weeks 5 days ago. Offline
Joined: 01/08/2010
bOt Points: 70

i created a package called MyPack and i dont knw how to put the created program in tht package directory....following is the code of the program....

 

package MyPack;

 

class Balance{

String name;

double bal;

 

Balance(String n,double b){

name=n;

bal=b;

}

void show(){

if(bal<0)

System.out.println("-->");

System.out.println(name+":$"+bal);

}}

class AccountBalance{

public static void main(String args[]){

Balance current[]=new Balance[3];

current[0]=new Balance ("K.J.Fielding",123.23);

current[1]=new Balance("Will Tell",157.02);

current[2]=new Balance("Tom Jackson",-12.33);

for (int i=0;i<3;i++)

current[i].show();

}}

shashwat
User offline. Last seen 6 hours 49 min ago. Offline
Joined: 02/18/2009
bOt Points: 1056
Managing Source and Class Files

Put the source code for a class, interface, enumeration, or annotation type in a text file whose name is the simple name of the type and whose extension is .java. For example:

 

//in the Rectangle.java file 

package graphics;

public class Rectangle {

   . . . 

}

Then, put the source file in a directory whose name reflects the name of the package to which the type belongs:

.....\graphics\Rectangle.java

The qualified name of the package member and the path name to the file are parallel, assuming the Microsoft Windows file name separator backslash (for Unix, use the forward slash).

class name graphics.Rectangle

pathname to file graphics\Rectangle.java

 

When you compile a source file, the compiler creates a different output file for each type defined in it. The base name of the output file is the name of the type, and its extension is .class. For example, if the source file is like this

 

//in the Rectangle.java file

package com.example.graphics;

public class Rectangle {

      . . . 

}


class Helper{

      . . . 

}

then the compiled files will be located at:

<path to the parent directory of the output files>\com\example\graphics\Rectangle.class

<path to the parent directory of the output files>\com\example\graphics\Helper.class

Like the .java source files, the compiled .class files should be in a series of directories that reflect the package name. However, the path to the .class files does not have to be the same as the path to the .java source files. You can arrange your source and class directories separately, as:

 

<path_one>\sources\com\example\graphics\Rectangle.java

 

<path_two>\classes\com\example\graphics\Rectangle.class

By doing this, you can give the classes directory to other programmers without revealing your sources. You also need to manage source and class files in this manner so that the compiler and the Java Virtual Machine (JVM) can find all the types your program uses.

The full path to the classes directory, <path_two>\classes, is called the class path, and is set with the CLASSPATH system variable. Both the compiler and the JVM construct the path to your .class files by adding the package name to the class path. For example, if

 

<path_two>\classes

is your class path, and the package name is

com.example.graphics,

then the compiler and JVM look for .class files in

<path_two>\classes\com\example\graphics.

A class path may include several paths, separated by a semicolon (Windows) or colon (Unix). By default, the compiler and the JVM search the current directory and the JAR file containing the Java platform classes so that these directories are automatically in your class path.