java package 命名空间
原文: 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中自己加了一段:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tellidea.run</groupId>
<artifactId>go</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>go</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</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.lang,java.utiletc. - 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.
package mypack;
public class employee
{
...statement;
}
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
//save as FirstProgram.java
package LearnJava;
public class FirstProgram{
public static void main(String args[]){
System.out.println("Welcome to package");
}
}
How to compile java package?
If you are not using any IDE, you need to follow the syntax given below:
javac -d directory javafilename
Example:
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
- 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 :
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
class B{
public static void main(String args[]){
pack.A obj = new pack.A();//using fully qualified name
obj.msg();
}
}Output:
Hello
- 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 :
//save by A.java
package pack;
public class A{
public void msg(){
System.out.println("Hello");
}
}
//save by B.java
package mypack;
import pack.A;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}Output:
Hello
- 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 :
//save by First.java
package LearnJava;
public class First{
public void msg(){System.out.println("Hello");}
}
//save by Second.java
package Java;
import Learnjava.*;
class Second{
public static void main(String args[]){
First obj = new First();
obj.msg();
}
}Output:
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.
// not allowed
import package p1.*;
package p3;
//correct syntax
package p3;
import package p1.*;
java package 命名空间的更多相关文章
- Atitit.软件命名空间 包的命名统计 及命名表(2000个名称) 方案java package
Atitit.软件命名空间 包的命名统计 及命名表(2000个名称) 方案java package 1. 统计的lib jar 列表1 2. Code3 3. 常用包名按找字母排序(2000个)4 ...
- 从零认识Java Package
Java Package为何被设计?如果你没想过,我这里或许可以提供一种视角. 想象一下,作为一个语言的设计者,你一定会考虑一个问题:变量名的冲突.为了解决这个问题,C++引入了命名空间(namesp ...
- JAVA package与import机制
JAVA package与import机制 http://files.cnblogs.com/files/misybing/JAVA-package-and-import.pdf import org ...
- java package(包)的用法
一般来说都用eclipse自动化图形工具搞定,我用的是ubuntu,所以需要自己打包引入. 什么是包? 这是对java源代码的组织和管理的一种方式,比如:当操作系统某个目录的文件非常多的时候,我们一般 ...
- Java package 包的命名规范。
Java的包名都有小写单词组成,类名首字母大写:包的路径符合所开发的 系统模块的 定义,比如生产对生产,物资对物资,基础类对基础类.以便看了包名就明白是哪个模块,从而直接到对应包里找相应的实现. 由于 ...
- java package 包 学习笔记
编译命令示例: javac -d . Main.java 注:带参数-d自动建立文件目录, 只使用javac 则需要手工创建目录 把 class文件打包 jar命令 jar cvf T.jar *; ...
- JAVA - package与import解析(一)
一.为什么要引入package和import?这个问题和c++中引入命名空间是一样的,也是为了解决重名问题.java通过包机制来解决重名问题,也就相当于给重名的代码加一系列前缀,从而达到唯一标识的作用 ...
- Java package详解
Java引入包(package)机制,提供了类的多层命名空间,用于解决类的命名冲突.类文件管理等问题.Java允许将一组功能相关的类放在同一个package下,从而组成逻辑上的类库单元.如果希望把一个 ...
- Java——package与import
[package] <1>为了解决类的命名冲突问题,Java引入包(package)机制,提供类的多重类命名空间. <2>package作为源文件的第一条语句(缺省时指定为 ...
随机推荐
- 自动调整速率的Actor设计模式
问题背景 与数据库或者存储系统交互是所有应用软件都必不可少的功能之一,akka开发的系统也不例外.但akka特殊的地方在于,会尽可能的将所有的功能都设计成异步的,以避免Actor阻塞,然而无法避免IO ...
- Kafka详解与总结(五)
Kafka持久化 1. 概述 Kafka大量依赖文件系统去存储和缓存消息.对于硬盘有个传统的观念是硬盘总是很慢,这使很多人怀疑基于文件系统的架构能否提供优异的性能.实际上硬盘的快慢完全取决于使用它的方 ...
- CSS知识点整理(2):框模型,定位
1. 框模型:Box Model 规定了元素处理元素框处理元素内容.外边距.边框.内边距的方式. 2. 当边距给定的值 可以小于4个.CSS定义了一些规则.处理这中情况: 如果缺少左外边距的值,则使用 ...
- android fragment轻松监听返回键/Fragment中的popupwindow响应返回键隐藏
现在的开发我们基本上都是一个主activity中放多个fragment,点击返回按钮的时候,直接退出主activity,但是我们在fragment中经常会弹出例如popupWindow这样的布局,用户 ...
- JS——对象创建
1.原始创建 <script> person = new Object();//不要var person.firstname = "Bill"; person.last ...
- c++和python如何实现主机字节序和网络字节序的相互转换
在上一篇文章网络编程:主机字节序和网络字节序中,介绍了主机字节序和网络字节序的基本概念以及在实际的编程中,何时需要进行网络字节序和主机字节序的转换.本篇文章着重介绍使用c++和python语言,如何实 ...
- java攻城狮之路--复习xml&dom_pull编程续
本章节我们要学习XML三种解析方式: 1.JAXP DOM 解析2.JAXP SAX 解析3.XML PULL 进行 STAX 解析 XML 技术主要企业应用1.存储和传输数据 2.作为框架的配置文件 ...
- Centos6.6 安装nfs网络文件系统
一.介绍 nfs网络文件系统的,大部分用在内网文件共享,比如,对集群上传文件做共享,经常用在图片部分,当然数据量大了还是要做分离,做为专门的接口比较好,介绍一下基本安装环境: 1)Cnetos6.6 ...
- unable to get system library for the project
当向eclipse导入项目实例后,项目上出现红叉的错误提示,在项目属性里的Java Build Path里发现了错误提示复选选项: unable to get system library for t ...
- tidyverse生态链
一套完整的数据分析流程 , 如下图所示 从图中可以看到,整个流程包括读取数据,整洁数据,数据探索和交流部分.经过前两部分, 我们可以得到一个整理好的数据,它的每一行都是一个样本 , 每一列是一个变量. ...