Package and classpath (the first edition of the Jingshui Flower)
This is my reading note, I hope to learn Java to learn Java. All code has been tested, the test environment:
Java Version "1.4.0-RC" Java (TM) 2 Runtime Environment, Standard Edition (Build 1.4.0-RC-B91) Java Hotspot (TM) Client VM (Build 1.4.0-RC-B91, MIXED MODE)
If you find any mistakes, or if you have any opinions, please don't know.
Quietwater 2003.2.4
Original intention
The first is that I have encountered questions about Package and ClassPath. After the help of netizens and their own exploration, I finally figured out the reason. Later, I found some netizens to have the same problems with me, so I decided to write articles to share with you.
text:
One. Differences of PRIVATE, Friendly, Protected, PUBLIC:
Private as the name suggests is private, so he uses himself. Including private member variables and private member functions, only members of the members they belong (including public Friendly Protected and private can be called. Public is a common, everyone, can be called by any Class. Friendly is friendly, can be called by the Class in the same package. If there is no clear declaration that belongs to which pack package, you can only be called in the same directory. Because the Class in the default is the same package belongs to the same package. Protected protected, it has the properties of Friendly, that is to say it is a super collection of Friendly, protected can also be accessed by subclavab.
II. Default package and friendly:
By default, all CLASs in the same directory are in the same package. So we can call Class in the same directory by default. By default they are Friendly. If it is J2SDK1.4, then we can do not configure ClassPath. But if you want Import your own package, then you must configure ClassPath.
3. Where is the problem?
for example:
D: /mypackage/a.java
Package mypackage; public class a {public a () {system.out.println ("Create A");}}
D: /mypackage/b.java
Import mypackage. *; class b {b () {system.out.println ("create b");} public static void main (string [] args) {a a = new a (); b b = new b ( );} First set classpath = .; d: / indicates that the current directory and the D disk root directory lookup package, the package name is consistent with the directory name. For example, MyPackage is found in D: / mypackage java to find D: / then find the d: / mypackage directory in the D: / mypackage directory by the package name, find a.class in the D: / mypackage directory
D: / mypackage> javac a.java No problem D: / mypackage> javac b.javab.java: 7: Cannot Access Abad class file: ./a.classclass file contains WRONG Class: mypackage.Aplease Remove Or Make Sure IT Appears In The Correct Subdirectory of The ClassPath. A A = New A (); ^ 1 Error moves B.java to D: /B.java and then run
D: /> Javac B.Java
D: /> Java BCREATE ACREATE B
everything is normal.
Summary: Because B Class is not in MyPackage, that is, B Class To use the class in MyPackage, Class B should be located outside the MyPackage package to avoid conflicts. Otherwise the default class b is located in the same package with the Class of its directory.
If you want to run the classes in the package, as follows:
D: / mypackage> java AException in thread "main" java.lang.NoClassDefFoundError: A (wrong name: mypackage / A) at java.lang.ClassLoader.defineClass0 (Native Method) at java.lang.ClassLoader.defineClass (ClassLoader. java: 509) at java.security.SecureClassLoader.defineClass (SecureClassLoader.java:123) at java.net.URLClassLoader.defineClass (URLClassLoader.java:246) at java.net.URLClassLoader.access $ 100 (URLClassLoader.java:54) at java.net.URLClassLoader $ 1.run (URLClassLoader.java:193) at java.security.AccessController.doPrivileged (Native Method) at java.net.URLClassLoader.findClass (URLClassLoader.java:186) at java.lang.ClassLoader. loadClass (ClassLoader.java:306) at sun.misc.Launcher $ AppClassLoader.loadClass (Launcher.java:265) at java.lang.ClassLoader.loadClass (ClassLoader.java:262) at java.lang.ClassLoader.loadClassInternal (ClassLoader .java: 322)
Write a full name package class name:
D: / mypackage> java mypackage.aexception in thread "main" java.lang.nosuchmethoder: main
Rewind A. Java as follows, add main ()
Package mypackage; public class a {public a () {system.out.println ("create a");} public static void main (string [] args) {a a = new a ();}} D: / mypackage > java mypackage.acreate a