Java_se Foundation--56. Package creation

Source: Internet
Author: User

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, then the class name is also called Demo1, but the file is called 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 run the Demo1 result? Output: This is the main method of Demo1, or output: This is the main method of Demo2.

Operation 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 resolve the class name to repeat the problem.

The role of the package:

1. Resolve the problem of duplicate class names that create conflicts.

2. Easy release of software version.

Concept of package:

Is there a folder in Windows where you can't have two files of the same name, and how does that work in Windows? That is creating folders, so packages in Java are equivalent to Windows folders.


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 a Java file.
2. If a class adds a package statement, the class's full class name is: Package name. class Name
3. A single Java file can have only one package statement.


The following section of code to create a package try:

Package A;class Demo1 {public static void main (string[] args) {System.out.println ("This is the main method of Demo1 ...");}
Then compile the results such as:


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 folder called A, drag the demo1.class in,


And then run


New Folder method is not very annoying, then I continue to say another way ~

Javac-d the storage path of the specified class file Java source file

or javac-d. Java source file (. means the current folder path, which is the path of the Demo1)



AC Penguin: 654249738, and self-taught Exchange group: 517284938


Java_se Foundation--56. Package creation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.