JAVA---Package and import parsing

Source: Internet
Author: User

Java for a long time, but the Java package and import understanding is not deep enough, today a good look at this knowledge point.
Why are package and import introduced?
This problem is the same as the introduction of namespaces in C + +, and also to solve the problem of duplicate names. Java through the package mechanism to solve the problem of duplicate name, it is equivalent to my current code to add a series of prefixes, from
and to achieve the role of a unique identity.
This is a bit like the name of a foreigner: if my name is Jeason, my dad's name is Kevin, my grandfather's name is Bob, and my grandfather's father is Peter.
so if no one and I have the same name (Jeason), then Jeason can uniquely identify me, and if there is a person also called Jeason, then I add my father's name Kevin, I will
it is Devin Jeason; if that person's father is also called Kevin, then I add my grandfather's name, I call Bob Devin Jeason, if his father and grandfather's name and my father and grandfather's
name is the same, then I add my father-in-law name, I call Peter Bob Kevin Jeason ...
this solves the problem of duplicate names.
Here are the specific uses:
I created a cat.java on the desktop with the following content:

 Public class cat{    publicstaticvoid  main (string[] args)    {        System.out.println ("This is a cat!" );    }}

This, of course, can be compiled and run correctly.
Now I'll add a package to the front and pack this code:

 Package Com.crazyacking.github;  Public class cat{    publicstaticvoid  main (string[] args)    {        System.out.println ("This is a cat!" );    }}

Compiled, but prompts "error: The main class Cat cannot be found or cannot be loaded" while executing the program.
Why does this problem occur? Because we have packaged the cat class, the system is naturally unable to find the cat class.

Now I'm going to create a dog.java on the desktop with the following:

 public  class   dog{ public  static  void   Main (string[] args) {Cat c  =new  
   
     Cat (); System.out.println ( "This is a dog!"     
    ); }}    
   

It will be displayed at compile time: The Cat class could not be found, or the same problem; we have packaged the Cat.java class.
So how can we access the Cat.java class?
In fact, in the Java language, the package is the directory corresponding to the file, we write the program at any time to the code with the file.
When we encapsulate the class, there is usually a default rule, that is, the format of the domain name, the domain name upside down to package the class.
Now that we have packaged the class and the package is corresponding to the file, we'll set up a folder to put it in, and put the. class file under the. com/crazyacking/github/folder.


The code becomes:

 Public class dog{     publicstaticvoid  main (string[] args)     {         Com.crazyacking.github.Cat C=new  com.crazyacking.github.Cat ();          System.out.println ("This is a dog!" );     }}

This gives you access to the Cat class. However, every time you visit the Cat class to write a lot of things, not only to write trouble, watching also annoying, when the import comes in handy.

Import Com.crazyacking.github.Cat;  Public class dog{     publicstaticvoid  main (string[] args)     {         Cat c= New Cat ();          System.out.println ("This is a dog!" );     }}

Of course, you can also include all the subclasses in Com.crazyacking.github:

import com.crazyacking.github.*;  Public class dog{     publicstaticvoid  main (string[] args)     {         Cat c= New Cat ();          System.out.println ("This is a dog!" );     }}

Note: the ". *" can only contain classes under the folder, but not the subfolders under the file.
With the package and import, we can solve the problem of duplicate name perfectly.

JAVA---Package and import parsing

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.