Package Usage


Test.java in folder s
package s;

import s.a.*;

public class Test extends CPubInAPkg {
   public static void main(String[] args) {
      CPubInAPkg capub = new CPubInAPkg();
      System.out.println(capub);
      
      //Not compilabe since
      //CInAPkg ca = new CInAPkg();
      //System.out.println(ca);
      
      Test t = new Test();
      System.out.println(t);
      
   }
}

CPubInAPkg.java in folder s\a
package s.a;

// public class can be accessed from outside package a 
// No constructor defined, the default constructor should has same 
// accessibility as the class, i.e. public

public class CPubInAPkg {
   int x = 12;
   int y = 24;
   public String toString() {
      return "(" + x + ", " + y + ")";
   }
}

// Default or friendly access class cannot be accessed from outside package a
// No constructor defined, the default constructor should has same 
// accessibility as the class, i.e. default or friendly

class CInAPkg {
   int x = 12;
   int y = 24;
   public String toString() {
      return "(" + x + ", " + y + ")";
   }
}

How to compile the above files using javac only (Not using any IDEs)? How to run it?
  • Compile from the parent folder of s.
  • javac s\a\CPubInAPkg.java
  • javac s\Test.java
  • java s.Test
    Last updated: 12-10-2002
    Copyright © 1999 - 2003 Roseanne Zhang, All Rights Reserved
    [ Java certification page] [ SCJP FAQ] [ JavaChina]