This features resembles to a namespace of C#. |
Key features
- Scope of public class that would be accessed by an external client.
- how to create and use a package!
- External classes did not throw error, and wont exit either unless
you put a value see in the next example.
|
Create a sub directory
 |
Server side coding, that will be used by an client.
package server_package;
import java.io.*;
//creating a package
// javac serve_pkg.java
public class serve_pkg
{
//constructor
public serve_pkg(){ System.out.println("Package Server-DF Constructor
responded");}
public serve_pkg(String str, int i)
{ System.out.println("Package Server Constructor responded");
System.out.println("This from Constructor : " +str + i);
}
//these are the fileds
//and fields properties
//private int n1 = 0;
//public String str="";
//method starts
//accesor type public
//return type void
//() place holder for parameters
public void process(String str2, int n2) throws IOException
{
System.out.println("This from serve_pkg : " +str2 + n2);
}//end of process
}//end of class
|
|
Save the code
 |
Compile 
|
Add the client code in the parent directory, as show in one of the
images.
import java.io.*;
import java.util.*;
import server_package.*;
// javac serve_pkg_client.java
public class serve_pkg_client
{
public static void main(String args[])throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
serve_pkg ss = new serve_pkg();
System.out.println("Client Starts");
//creating object where ss is reference to the class simple server_1
String str ="";
int n1 = 0;
//Static String str; //
//Static int n1;
try {
System.out.print("Please type your name :");
str = br.readLine();
String name = str;
System.out.print("Please type age:");
str = br.readLine();
n1 = Integer.parseInt(str);
if ((str !="") & (n1 >0 ))
{
ss.process(name, n1);
System.out.println("Client ends");
}
}
catch(Exception e) { System.out.println("data was blank");}
}//end of main
}//end of class
|
 |
compile creates a class

|
 |
Note:
Scanner sc = new Scanner(System.in);
System.out.print("Please type your name :");
str = sc.nextLine();
System.out.print("Please type age:");
n1 = sc.nextInt();
str += " : " + n1;
System.out.println();
|
The above method of data input, somehow did not throw any error when the
values were null or empty. |