In a Java Project, does keeping all the .java files in the same folder mean they are in the same package?

What is the difference in making a Package for our project compared to keeping all the project files in one folder?

This thread doesn't really address my question.

解决方案

There is a relationship between package and directory, but it's one that you must maintain. If you have a class that's in "mypackage1.mypackage2", that means that the java command is going to expect to find it in a directory structure named "mypackage1\mypackage2" (assuming "backwards" Windows notation), with that directory structure further embedded in a directory (let's call it "myjava") whose name is in the classpath (or else is directly in the "current directory").

So your Java class (which internally says package mypackage1.mypackage2;) is in, say, "\Users\myName\myjava\mypackage1\mypackage2\", and you place "\Users\myName\myjava" in the class path, or else you have your current directory set to "\Users\myName\myjava".

If you mix this up, either the class will not be found at all, or you will get an error something like the ever-nebulous "NoClassDefFoundError".

As to why one would use packages (and directories), the reason has to do with "name space" and "separation of concerns" (look those up). Java would be much harder to keep straight if there were no packages and all the "java.lang", "java.io", "sun.misc", et al classes were together. First off, one would have to use name "prefixes" to keep them straight and avoiding name conflicts. And much of the logical grouping would be lost.

With your own projects you don't need to use packages for simple little programs you write for yourself, but if you write something you might give to someone else it's polite to use a package such as "myname.myproject" (substituting your name and project of course), so the person you give it to can combine it with others without name conflicts.

In large applications you'll find using further levels of separation helps you keep the functions straight, so you know where everything is. It also discourages you from "crossing the boundary" between different functional areas, so you don't get unrelated logic entertwined.

Eclipse (if you use that) kind of muddles the issue a bit because it "wants" to provide directory and package names and will sometimes (but not always) keep them in sync.

java 包和目录,Java中包和目录的区别相关推荐

  1. java中的包如何定义_java中包的定义与使用

    包的本质就属于一个文件夹,用来解决类名称重名的问题. 包的定义:在源文件首行使用package包名称: package www.myfile.java; public class Test{ publ ...

  2. java包 类 方法_Java中包与包之间方法的调用及其关键字区分(基础)

    最近在写应用时,对包与包之间方法的调用不太清楚,专门看视频和网站后整理了这篇文章,希望对刚入门的同志有所帮助,进入主题. 首先我们先简单看看Java中private.protected.public和 ...

  3. jsp与java的关系_jsp教程中:jsp与servlet的区别和关系

    jsp教程上:我们一定学了servlet,也学习了jsp,那么jsp与servlet的区别是什么呢,jsp与servlet又有什么关系呢?下面我们先创建一个demo.jsp的页面,然后tomcat运行 ...

  4. python怎么导入包-如何理解Python中包的引入

    Python的from import *和from import *,它们的功能都是将包引入使用,但是它们是怎么执行的以及为什么使用这种语法呢? 从一模块导入全部功能 from import * me ...

  5. java iter是否存在_Java中ListIterator和Iterator的区别以及ListIterator的应用

    Java中ListIterator和Iterator详解与辨析 在使用java集合的时候,都需要使用Iterator.但是java集合中还有一个迭代器ListIterator,在使用List.Arra ...

  6. java堆和客栈_java中堆和栈的区别分析

    堆和栈是java数据结构里非常重要的概念,本文较为详细的分析了二者之间的区别.供大家参考.具体如下: Java的堆是一个运行时数据区,类的(对象从中分配空间.这些对象通过new.newarray.an ...

  7. java包不正确_java中包容易出现的错误及权限问题

    /* 3,权限在不同地方的体现: public protected default private 同一类中: ok ok ok ok 同一包中: ok ok ok 子类中: ok ok 不同包中: ...

  8. endorsed java_关于java:在jdk1.6中使用Endorsed目录的确切方法是什么

    我想将jaxws升级到2.2(jdk1.6与jaxws 2.1捆绑在一起).我的jdk是(我没有安装public jre): java version"1.6.0_20" Java ...

  9. python导包的问题_python中包引入遇到的问题

    最近在用python的flask时候发现一点问题,代码组织如下 |-app |-__init__.py |-views.py |-operations.py |-xxx.py |-run.py 按照p ...

  10. java变量存储位置_java 中变量存储位置的区别

    [原文] 1.寄存器:最快的存储区, 由编译器根据需求进行分配,我们在程序中无法控制. 2. 栈:存放基本类型的变量数据和对象的引用,但对象本身不存放在栈中,而是存放在堆(new 出来的对象)或者常量 ...

最新文章

  1. 凝聚406万开发者 飞桨十大发布提速产业智能化
  2. 张首晟生前重磅演讲:要用第一性原理的思维方式来理解今天的世界
  3. Android --- ImageView的scaleType属性讲解
  4. flash咏鹅课件_咏鹅flash动画课件下载
  5. android 录音命令,音频延迟  |  Android NDK  |  Android Developers
  6. android奔溃日志手机查看
  7. keep-alive + vuex + mint + Infinite scroll 保存分页列表数据
  8. 参数方法(parameter)与非参数方法(nonparameter)
  9. 集异璧(GEB)第一章WU谜题读后感
  10. 英文视频字幕自动生成
  11. ViewBinding
  12. “Only fullscreen opaque activities can request orientation “问题分析
  13. 软著申请个人实名认证流程
  14. STM32唯一序列号UID--HAL_GetUIDw0详解
  15. U盘文件丢失选择什么数据恢复软件好
  16. 自动驾驶汽车如何识别红绿灯?三个角度告诉你答案丨曼孚科技
  17. 弱网测试(ios手机自带)
  18. java数组元素替换操作_js数组操作(添加、删除、替换元素)
  19. 通过聚合数据API实现快递数据查询-短信验证码-企业核名
  20. ESP32 WIFI MESH学习笔记7-物联网与控制(ESP32部分)

热门文章

  1. 【2019年百度度秘事业部秋招】语音算法岗面经(一面+二面)无三面机会
  2. 蓝牙路由器物联网优势是什么?
  3. hexo和coding打造个人博客
  4. python代码直接关机_Python实现自动关机
  5. ggplot2 标题:主、轴和图例标题
  6. 骑士 74CMS V3.4漏洞
  7. 我愿执笔弃花间,此后,离经易道为一人
  8. 【初识太极】CAD用到的各种文件格式,你都知道吗?
  9. 推荐文章之从技术、平台、工具、语言框架等四大方面,详解技术未来的趋势
  10. 趣头条广告如何定向推广?趣头条广告投放有效果吗?