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中自己加了一段:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <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.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.
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.*;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现