Packages in Java

like namespaces ?

import package.name.Class;   // Import a single class
import package.name.*;   // Import the whole package

Import a Class

import java.util.Scanner;
import java.util.*;

User-defined Packages

//save by A.java  
package pack;

public class A {
    public void msg() {
        System.out.println("Hello");
    }
}
//save by B.java  
package mypack;

import pack.*;

class B {
    public static void main(String args[]) {
        A obj = new A();
        obj.msg();
    }
}

Last updated