Using Classes from Other Packages

• One of the 3 ways can be used:
   • Import the package member using import
     statement
   • Import the member's entire package using
     import statement
   • Refer to the member by its fully qualified name
     (without using import statement)




                                                       1
Importing Classes from Packages
• To use classes from other package, you need to
  import the package of those classes
• By default, all Java programs import the
  java.lang.* package
   • you can use classes like String and Integer
     inside the program even though you haven't
     imported any packages

• The import statement is used to import classes from
  packages
• There can be any number of import statements in a
  source file
                                                    2
Importing Classes from Packages Contd..

• The syntax of import statement is

              import <nameOfPackage>;

   • nameOfPackage is any user defined or system
     defined package name
   • eg. :
      • // Importing a class a Date class
      • import java. util. Date
      • // Importing all classes in java. util package
      • import java. util.*;
                                                         3
import javax.swing.*;               Example   Program
public class SumArray {
  public static void main( String                  Output
   args[] ) {
   int a[] = { 1, 2, 3, 4, 5, 6};
   int total = 0;
   for ( int i = 0; i < a.length; i++ )
     total += a[ i ];

        OptionPane.showMessageDialog
        ( null, "Total of array elements: "        Importing entire
        + total, "Sum the Elements of an           package
        Array", JOptionPane.INFORMATI
        ON_MESSAGE );
        System.exit( 0 );
    }
}

                                                                      4
Another Example Program
package MyPack;              Package in
public class Balance {       which Balance                                       Test is other
    String name;             class is defined     package test;                  package
    double bal;
                                                  import MyPack.Balance;
    public Balance(String n, double b) {
       name = n;                                                              Importing a
                                                  class TestBalance {         package member
     bal = b;                                       public static void main(String[ ] args)
  }                                                 {
    public void show() {                                 Balance test Bal=
     if(bal<0)                                                   new Balance("J.
     System .out.print("-->> ");                                   J. Jaspers", 99.88);
     System.out.println(name + ": $" +                 testBal.show();
           bal);                                     }
    }                                             }
}
                                             Output




                                                                                           5
Fully Qualified Name
• Another approach to use a class from other packages
• No import statement is used
• Fully Qualify the name of the class
• The following example illustrates:
  • public static void main(String[] args) {
  •      java.util.Date x = new java.util.Date();
  •      System.out.println(“Today’s Date : “+
    x.toString());
     }
                                            Date class is
                                            fully qualified



                                                              6
Example Program
package MyPack;                                  package test;
                           Package in
public class Balance {     which Balance                                       Test is other
    String name;                                 class TestBalance {           package
                           class is defined
    double bal;                                    public static void main(String[ ] args)
    public Balance(String n, double b) {           {
       name = n;                                        MyPack.Balance testBal =
     bal = b;                                             new MyPack.Balance("J.
  }                                                              J. Jaspers", 99.88);
    public void show() {                              testBal.show();
     if(bal<0)                                      }
     System.out.print("-->> ");                  }
                                                                         Fully qualifying class
     System.out.println(name + ": $" +                                   name
           bal);}}
                                        Output



                                                                                             7

Using classes from other packages

  • 1.
    Using Classes fromOther Packages • One of the 3 ways can be used: • Import the package member using import statement • Import the member's entire package using import statement • Refer to the member by its fully qualified name (without using import statement) 1
  • 2.
    Importing Classes fromPackages • To use classes from other package, you need to import the package of those classes • By default, all Java programs import the java.lang.* package • you can use classes like String and Integer inside the program even though you haven't imported any packages • The import statement is used to import classes from packages • There can be any number of import statements in a source file 2
  • 3.
    Importing Classes fromPackages Contd.. • The syntax of import statement is import <nameOfPackage>; • nameOfPackage is any user defined or system defined package name • eg. : • // Importing a class a Date class • import java. util. Date • // Importing all classes in java. util package • import java. util.*; 3
  • 4.
    import javax.swing.*; Example Program public class SumArray { public static void main( String Output args[] ) { int a[] = { 1, 2, 3, 4, 5, 6}; int total = 0; for ( int i = 0; i < a.length; i++ ) total += a[ i ]; OptionPane.showMessageDialog ( null, "Total of array elements: " Importing entire + total, "Sum the Elements of an package Array", JOptionPane.INFORMATI ON_MESSAGE ); System.exit( 0 ); } } 4
  • 5.
    Another Example Program packageMyPack; Package in public class Balance { which Balance Test is other String name; class is defined package test; package double bal; import MyPack.Balance; public Balance(String n, double b) { name = n; Importing a class TestBalance { package member bal = b; public static void main(String[ ] args) } { public void show() { Balance test Bal= if(bal<0) new Balance("J. System .out.print("-->> "); J. Jaspers", 99.88); System.out.println(name + ": $" + testBal.show(); bal); } } } } Output 5
  • 6.
    Fully Qualified Name •Another approach to use a class from other packages • No import statement is used • Fully Qualify the name of the class • The following example illustrates: • public static void main(String[] args) { • java.util.Date x = new java.util.Date(); • System.out.println(“Today’s Date : “+ x.toString()); } Date class is fully qualified 6
  • 7.
    Example Program package MyPack; package test; Package in public class Balance { which Balance Test is other String name; class TestBalance { package class is defined double bal; public static void main(String[ ] args) public Balance(String n, double b) { { name = n; MyPack.Balance testBal = bal = b; new MyPack.Balance("J. } J. Jaspers", 99.88); public void show() { testBal.show(); if(bal<0) } System.out.print("-->> "); } Fully qualifying class System.out.println(name + ": $" + name bal);}} Output 7