原文: http://www.studytonight.com/java/package-in-java.php

创建一个简单的maven 项目的命令是: mvn  archetype:generate -DgroupId=com.tellidea.run  -DartifactId=go -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

成功后,项目结构如下:

pom.xml中自己加了一段:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.tellidea.run</groupId>
  5. <artifactId>go</artifactId>
  6. <packaging>jar</packaging>
  7. <version>1.0-SNAPSHOT</version>
  8. <name>go</name>
  9. <url>http://maven.apache.org</url>
  10. <dependencies>
  11. <dependency>
  12. <groupId>junit</groupId>
  13. <artifactId>junit</artifactId>
  14. <version>3.8.1</version>
  15. <scope>test</scope>
  16. </dependency>
  17. </dependencies>
  18. <build>
  19. <plugins>
  20. <plugin>
  21. <groupId>org.apache.maven.plugins</groupId>
  22. <artifactId>maven-compiler-plugin</artifactId>
  23. <configuration>
  24. <source>1.8</source>
  25. <target>1.8</target>
  26. </configuration>
  27. </plugin>
  28. </plugins>
  29. </build>
  30. </project>

  执行编译:mvn compile 后,结构目录如下:

这两种方式都可以跑起来:

上面两种方式都可以运行 App.class成功!!

-----------------------------------------------------------------------------------

Java Package

Package are used in Java, in-order to avoid name conflicts and to control access of class, interface and enumeration etc. A package can be defined as a group of similar types of classes, interface, enumeration and sub-package. Using package it becomes easier to locate the related classes.


Package are categorized into two forms

  • Built-in Package:-Existing Java package for example java.langjava.util etc.
  • User-defined-package:- Java package created by user to categorized classes and interface


Creating a package

Creating a package in java is quite easy. Simply include a package command followed by name of the package as the first statement in java source file.

  1. package mypack;
  2. public class employee
  3. {
  4. ...statement;
  5. }

The above statement create a package called mypack.

Java uses file system directory to store package. For example the .class for any classes you define to be part of mypack package must be stored in a directory called mypack.

Additional points on package:

  • A package is always defined in a separate folder having the same name as a package name.
  • Define all classes in that package folder.
  • All classes of the package which we wish to access outside the package must be declared public.
  • All classes within the package must have the package statement as its first line.
  • All classes of the package must be compiled before use (So that its error free)

Example of java package

  1. //save as FirstProgram.java
  2. package LearnJava;
  3. public class FirstProgram{
  4. public static void main(String args[]){
  5. System.out.println("Welcome to package");
  6. }
  7. }
How to compile java package?

If you are not using any IDE, you need to follow the syntax given below:

  1. javac -d directory javafilename

Example:

  1. javac -d . FirstProgram.java

The -d switch specifies the destination where to put the generated class file. You can use any directory name like d:/abc (in case of windows) etc. If you want to keep the package within the same directory, you can use . (dot).

How to run java package program?

You need to use fully qualified name e.g. LearnJava.FirstProgram etc to run the class.

To Compile: javac -d . FirstProgram.java

To Run: java LearnJava.FirstProgram

Output: Welcome to package

The -d is a switch that tells the compiler where to put the class file i.e. it represents destination. The . represents the current folder.


import keyword

import keyword is used to import built-in and user-defined packages into your java source file so that your class can refer to a class that is in another package by directly using its name.

There are 3 different ways to refer to class that is present in different package

  1. Using fully qualified name(But this is not a good practice.)

    If you use fully qualified name then only declared class of this package will be accessible. Now there is no need to import. But you need to use fully qualified name every time when you are accessing the class or interface.

    It is generally used when two packages have same class name e.g. java.util and java.sql packages contain Date class.

    Example :

    1. //save by A.java
    2. package pack;
    3. public class A{
    4. public void msg(){System.out.println("Hello");}
    5. }
    6. //save by B.java
    7. package mypack;
    8. class B{
    9. public static void main(String args[]){
    10. pack.A obj = new pack.A();//using fully qualified name
    11. obj.msg();
    12. }
    13. }

    Output:

    1. Hello
  2. import the only class you want to use(Using packagename.classname)

    If you import package.classname then only declared class of this package will be accessible.

    Example :

    1. //save by A.java
    2. package pack;
    3. public class A{
    4. public void msg(){
    5. System.out.println("Hello");
    6. }
    7. }
    8. //save by B.java
    9. package mypack;
    10. import pack.A;
    11. class B{
    12. public static void main(String args[]){
    13. A obj = new A();
    14. obj.msg();
    15. }
    16. }

    Output:

    1. Hello
  3. import all the classes from the particular package(Using packagename.*)

    If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages.

    The import keyword is used to make the classes and interface of another package accessible to the current package.

    Example :

    1. //save by First.java
    2. package LearnJava;
    3. public class First{
    4. public void msg(){System.out.println("Hello");}
    5. }
    6. //save by Second.java
    7. package Java;
    8. import Learnjava.*;
    9. class Second{
    10. public static void main(String args[]){
    11. First obj = new First();
    12. obj.msg();
    13. }
    14. }

    Output:

    1. Hello

Points to remember

  • When a package name is not specified , a class is defined into the default package (the current working directory) and the package itself is given no name. Hence you were able to execute assignments earlier.
  • While creating a package, care should be taken that the statement for creating package must be written before any other import statements.
  1. // not allowed
  2. import package p1.*;
  3. package p3;
  1. //correct syntax
  2. package p3;
  3. import package p1.*;

java package 命名空间的更多相关文章

  1. Atitit.软件命名空间  包的命名统计 及命名表(2000个名称) 方案java package

    Atitit.软件命名空间  包的命名统计 及命名表(2000个名称) 方案java package 1. 统计的lib jar 列表1 2. Code3 3. 常用包名按找字母排序(2000个)4 ...

  2. 从零认识Java Package

    Java Package为何被设计?如果你没想过,我这里或许可以提供一种视角. 想象一下,作为一个语言的设计者,你一定会考虑一个问题:变量名的冲突.为了解决这个问题,C++引入了命名空间(namesp ...

  3. JAVA package与import机制

    JAVA package与import机制 http://files.cnblogs.com/files/misybing/JAVA-package-and-import.pdf import org ...

  4. java package(包)的用法

    一般来说都用eclipse自动化图形工具搞定,我用的是ubuntu,所以需要自己打包引入. 什么是包? 这是对java源代码的组织和管理的一种方式,比如:当操作系统某个目录的文件非常多的时候,我们一般 ...

  5. Java package 包的命名规范。

    Java的包名都有小写单词组成,类名首字母大写:包的路径符合所开发的 系统模块的 定义,比如生产对生产,物资对物资,基础类对基础类.以便看了包名就明白是哪个模块,从而直接到对应包里找相应的实现. 由于 ...

  6. java package 包 学习笔记

    编译命令示例: javac -d . Main.java 注:带参数-d自动建立文件目录, 只使用javac 则需要手工创建目录 把 class文件打包 jar命令 jar cvf T.jar *; ...

  7. JAVA - package与import解析(一)

    一.为什么要引入package和import?这个问题和c++中引入命名空间是一样的,也是为了解决重名问题.java通过包机制来解决重名问题,也就相当于给重名的代码加一系列前缀,从而达到唯一标识的作用 ...

  8. Java package详解

    Java引入包(package)机制,提供了类的多层命名空间,用于解决类的命名冲突.类文件管理等问题.Java允许将一组功能相关的类放在同一个package下,从而组成逻辑上的类库单元.如果希望把一个 ...

  9. Java——package与import

    [package]   <1>为了解决类的命名冲突问题,Java引入包(package)机制,提供类的多重类命名空间. <2>package作为源文件的第一条语句(缺省时指定为 ...

随机推荐

  1. Kafka详解与总结(三)

    Kafka分片存储机制 几个kafka重要概念: Broker:消息中间件处理结点,一个Kafka节点就是一个broker,多个broker可以组成一个Kafka集群. Topic:一类消息,例如pa ...

  2. yield from (python生成器)

    #生成器中的yield from是干什么用的(一般多用于线程,协程那)def func(): # for i in 'AB': # yield i yield from 'AB' # 就相当于上面的f ...

  3. docker容器如何安装vim

    mv /etc/apt/sources.list /etc/apt/sources.list.bak && \     echo "deb http://mirrors.16 ...

  4. cocos2d-x 不规则碰撞检测 【转载】

    原文:http://www.2cto.com/kf/201401/272331.html //判断有没有点到有材质的部分, p_point相对, CCSprite坐标  (p_point是相对 Spr ...

  5. 设置靠近 水平居中的主体内容Div 的 左侧位置固定的Div

    示例效果: 1.主体内容的divMain 水平居中: 2.divLeft 靠近divMain ,位置固定,不随垂直滚动条而动: 相关代码: <html> <head runat=&q ...

  6. SQL Sever语言 事务

    事务:保障流程的完整执行保证程序某些程序在运行时同时成功同时失败,保证程序的安全性 begin tran --在流程开始的位置加 --此处写SQL语句 if @@error>0 --ERRORS ...

  7. JavaScript 封装插件学习笔记(一)

    此篇只是笔记,在借鉴.参考.模仿的过程,可能不完整,请多指教! 定义插件名称要注意命名冲突,防止全局污染. 1.第一种Javascript对象命名:(Javascript语言是“先解析,后运行”,解析 ...

  8. JS——事件冒泡与捕获

    事件冒泡与事件捕获 1.冒泡:addEventListener("click",fn,false)或者addEventListener("click",fn): ...

  9. CSS——属性选择器

    属性选择器:通过对标签中属性的选择,控制标签. <!DOCTYPE html> <html> <head> <style> div[class*=&qu ...

  10. log4j最全教程

    (转自http://www.codeceo.com/article/log4j-usage.html) 日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供方 ...