Java Packages

  1. Core packages in J2SE 6
  2. Pre defined Packages - Call a Class within the File itself in Java
  3. User defined Package - Call a Class within the Package in Java
  4. User defined Package - Call another Package Class in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Core packages in J2SE 6


java.lang	-	basic language functionality and fundamental types
java.util	-	collection data structure classes
java.io		-	file operations
java.math	-	multiprecision arithmetics
java.nio	-	the New I/O framework for Java
java.net	- 	networking operations, sockets, DNS lookups, ...
java.security	- 	key generation, encryption and decryption
java.sql	- 	Java Database Connectivity (JDBC) to access databases
java.awt	- 	basic hierarchy of packages for native GUI components
javax.swing	- 	hierarchy of packages for platform-independent rich GUI components
java.applet	- 	classes for creating an applet





B. Pre defined Packages - Call a Class within the File itself in Java


Package_Ex1.java

package balaji.samplecodez.java;
 
// The below packages are J2SE pre defined packages..
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
 
public class Package_Ex1 {
 
	Scanner scan;
	InputStreamReader isr;
	int num;
	String name;
	
	void getVal() {
		
		System.out.println("Java Pre defined Packages " +
				"- Call a Class within the File itself");
		System.out.println("\nEnter a number :");
		scan = new Scanner(System.in);
		num = scan.nextInt();
		
		System.out.println("\nEnter a String :");
		BufferedReader br = new BufferedReader
				(new InputStreamReader(System.in));
		
		try {
			
			name = br.readLine();
		} 
		catch (IOException e) {
			
			e.printStackTrace();
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Package_Ex1 obj = new Package_Ex1();
		
		obj.getVal();
	}
}

Sample Output

Java Pre defined Packages - Call a Class within the File itself

Enter a number :
111

Enter a String :
Balaji




C. User defined Package - Call a Class within the Package in Java


Package_Ex2.java

package balaji.samplecodez.java;
 
import java.util.Scanner;
 
public class Package_Ex2 {
 
	public int a, b, c;
	Scanner scan;
	
	public void getVal() {
		
		System.out.println("Java User defined Package " +
				"- Call a Class within the Package");
		scan = new Scanner(System.in);
		
		System.out.println("\nEnter value of 'a' :");
		a = Integer.parseInt(scan.nextLine());
		
		System.out.println("Enter value of 'b' :");
		b = Integer.parseInt(scan.nextLine());
	}
	
	public void add() {
				
		c = a + b;
	}
	
	public void display() {
		
		System.out.println("\nThe Sum is : " + c);
	}
}

MainClass.java

package balaji.samplecodez.java;
 
public class MainClass {
 
	public static void main(String args[]) {
		
		Package_Ex2 obj = new Package_Ex2();
		
		obj.getVal();
		obj.add();
		obj.display();
	}
}

Sample Output

Java User defined Package - Call a Class within the Package

Enter value of 'a' :
11
Enter value of 'b' :
20

The Sum is : 31




D. User defined Package - Call another Package Class in Java


Package_Ex3.java

package balaji.samplecodez.java;
 
import java.util.Scanner;
 
public class Package_Ex3 {
 
	public int a, b, c;
	Scanner scan;
	
	public void getVal() {
		
		System.out.println("Java User defined Package " +
				"- Call another Package's Class");
		scan = new Scanner(System.in);
		
		System.out.println("\nEnter value of 'a' :");
		a = Integer.parseInt(scan.nextLine());
		
		System.out.println("Enter value of 'b' :");
		b = Integer.parseInt(scan.nextLine());
	}
	
	public void add() {
				
		c = a + b;
	}
	
	public void display() {
		
		System.out.println("\nThe Sum is : " + c);
	}
}

MainClass.java

package balaji.scz.java;
 
import balaji.samplecodez.java.Package_Ex3;
 
public class MainClass {
	
	public static void main(String args[]) {
		
		Package_Ex3 obj = new Package_Ex3();
		
		obj.getVal();
		obj.add();
		obj.display();
	}
}

Sample Output

Java User defined Package - Call another Package's Class

Enter value of 'a' :
113
Enter value of 'b' :
148

The Sum is : 261






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com