Next I'll tell you ... Package.
Look at a piece of code first
Class Demo1{public static void Main (string[] args) {System.out.println ("This is the main method of Demo1");}}
then create a new Java file, and the class name is also called Demo1. But the file name is Demo2 .
Class Demo1 {public static void main (string[] args) {System.out.println ("This is the main method of Demo2");}}
What if I compile Demo1 and then compile Demo2 and then execute the Demo1 result? Output: This is the main method of Demo1, or output: This is the main method of Demo2.
Execution Result:
This is the main method of Demo2.
Have you ever thought about why???
Because, after compiling this file is not also will produce demo1.class of this file after compiling will overwrite the previous compilation
Package: You can solve the problem of class name repetition.
The role of the package:
1. Resolve the problem of recurring conflicts in the class name.
2. Facilitate the publication of the software version number.
Concept of package:
Is Windows a directory not able to appear two of the same name of the file, that in Windows how to solve? That is to create the directory, so the package in Java is equivalent to the Windows directory.
To define the format of the package:
Package name.
Things to note in a package statement:
1. The package statement must be in the first statement in the Java file.
2. Suppose that a class adds a package statement. Then the complete class name of the class is: Package name. class Name
3. A Java file can have only one package statement.
Try to create a package with a snippet of code:
Package A;class Demo1 {public static void main (string[] args) {System.out.println ("This is the main method of Demo1 ...");}
The results are then compiled, for example, by:
What's the meaning of this hint? Exception: Noclassdeffounderror; The meaning of the class name is not found
We look at the 2nd of matters to note in the package statement. The complete class name for this class is: A. Demo1
First create a directory called A. Drag the Demo1.class in,
And then execute
The method of creating a new directory is not considered very annoying. Then I'll go on to say the second way ~
Javac-d the storage path of the specified class file Java source file
or javac-d. The Java source file (. means the current directory path, which is the path of the Demo1)
AC Penguin: 654249738, and self-taught Exchange group: 517284938
Java_se base--56. Package Creation