{"id":59,"date":"2020-12-31T22:01:09","date_gmt":"2020-12-31T22:01:09","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/practical-java-programming\/?page_id=59"},"modified":"2021-01-09T22:08:49","modified_gmt":"2021-01-09T22:08:49","slug":"packages","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/packages\/","title":{"rendered":"Packages"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_83 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a06ee0a1fd14\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a06ee0a1fd14\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/packages\/#Name_Spaces_and_Reusability\" >Name Spaces and Reusability<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/packages\/#Qualified_Names\" >Qualified Names<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/packages\/#The_import_Statement\" >The import Statement<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/packages\/#Static_imports\" >Static imports<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/packages\/#Automatic_import\" >Automatic import<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/packages\/#The_Default_Package\" >The Default Package<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/packages\/#The_Current_Package\" >The Current Package<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/packages\/#The_package_Statement\" >The package Statement<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/packages\/#Compilation_Units\" >Compilation Units<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/packages\/#Host_Support_for_Packages\" >Host Support for Packages<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/packages\/#Type_Definitions\" >Type Definitions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/packages\/#Access_Control\" >Access Control<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/packages\/#Access_to_Package_Contents\" >Access to Package Contents<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/packages\/#Visibility_Modifiers\" >Visibility Modifiers<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Name_Spaces_and_Reusability\"><\/span>Name Spaces and Reusability<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>A major problem when developing reusable code is that of&nbsp;<em><strong>name conflicts<\/strong><\/em>.<\/p>\n\n\n\n<p>Vendor B might develop a library of classes, while Vendor M might develop another library of classes. Customer C wants to use both libraries, but discovers that a number of class names have been duplicated across the two libraries.<\/p>\n\n\n\n<p>What to do?<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Qualified_Names\"><\/span>Qualified Names<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>A solution is to organize code into named&nbsp;<em>packages<\/em>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>In C++, the feature equivalent to Java&#8217;s packages is C++&nbsp;<em>namespaces<\/em>.<\/p><\/blockquote>\n\n\n\n<p>Then, when class names conflict among different code libraries, we can explicitly place the code in separate packages, and qualify the name references with the appropriate package name.<\/p>\n\n\n\n<p>The namespace represented is arranged hierarchically in terms of&nbsp;<em>packages<\/em>&nbsp;and&nbsp;<em>sub-packages<\/em>&nbsp;of a package, etc. For example, here is a reference to a method in the Java class library:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>java.lang.String.substring()<\/strong><\/pre>\n\n\n\n<p>where:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>java<\/strong>&nbsp;is the package name<\/li><li><strong>lang<\/strong>&nbsp;is the sub-package name<\/li><li><strong>String<\/strong>&nbsp;is the class name<\/li><li><strong>substring<\/strong>&nbsp;is the method name<\/li><\/ul>\n\n\n\n<p>And here is a reference to a class library method for a (mythical) vendor, <strong>Aardvark<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>COM.Aardvark.eng.gui.Menu.add(String s)<\/strong><\/pre>\n\n\n\n<p>where:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>COM.Aardvark<\/strong>&nbsp;is from the vendor&#8217;s Internet domain name,&nbsp;<strong>aardvark.com<\/strong><\/li><li><strong>eng.gui<\/strong>&nbsp;is the vendor&#8217;s internal naming convention:<ul><li><strong>eng<\/strong>&nbsp;&#8212; Engineering<\/li><li><strong>gui<\/strong>&nbsp;&#8212; Graphical User Interface development<\/li><\/ul><\/li><li><strong>Menu<\/strong>&nbsp;is the class name<\/li><li><strong>add<\/strong>&nbsp;is the method name within that class<\/li><\/ul>\n\n\n\n<p>Note that the convention is to use the organization&#8217;s Internet domain names&nbsp;<em>in reverse order<\/em>. For example, common names might look like:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>COM.Microsoft...\nCOM.Oracle...\nCOM.Borland...\nCOM.Symantec...<\/strong><\/pre>\n\n\n\n<p>However, lots of vendors omit the&nbsp;<strong>COM.<\/strong>, or just use their company name as the first name.<\/p>\n\n\n\n<p>Also, some vendors use&nbsp;<code><strong>com<\/strong><\/code>, rather than&nbsp;<code><strong>COM<\/strong><\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_import_Statement\"><\/span>The import Statement<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>It&#8217;s a royal pain to have to fully qualify every class name in your program.<\/p>\n\n\n\n<p>So the Java folks added the&nbsp;<strong><em>import statement<\/em><\/strong>&nbsp;as a convenience:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>import java.util.Hashtable; \/\/ import just HashTable<\/strong><\/pre>\n\n\n\n<p>or:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>import java.util.*; \/\/ import all classes in java.util<\/strong><\/pre>\n\n\n\n<p>which allows the Java compiler to make a name accessible through an abbreviated name &#8212; the class name itself (in this case&nbsp;<strong>Hashtable<\/strong>).<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong><em>Note:<\/em><\/strong>&nbsp;I try to discourage the use of the &#8216;<strong>*<\/strong>&#8216; method of importing, because I consider it bad &#8212; often sloppy\/lazy &#8212; software engineering practice.&nbsp;&nbsp;<em><strong>Please don&#8217;t use it, at least in my course!<\/strong><\/em><\/p><\/blockquote>\n\n\n\n<p>If you wish, you can use a mix of import statements and fully qualified references.<\/p>\n\n\n\n<p>If two packages imported in this way contain classes with the same name, you cannot use either of those classes without using the fully qualified name for each.<\/p>\n\n\n\n<p><strong><em>Note:<\/em><\/strong>&nbsp; Remember:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>There may be any number of <strong>import<\/strong> statements in a Java program.<\/li><li>They must follow any&nbsp;<strong>package&nbsp;<\/strong>statement in the source<\/li><li>They must precede the first&nbsp;<strong>class&nbsp;<\/strong>definition (or&nbsp;<strong>interface&nbsp;<\/strong>definition &#8212; more later) in the file.<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Static_imports\"><\/span>Static imports<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Starting in Java 5, the import statement was enhanced to allow the importing of static methods and fields from a class.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [1,2,3,4]; title: ; quick-code: false; notranslate\" title=\"\">\nimport static java.lang.System.exit;\nimport static java.lang.System.out;\nimport static java.lang.Math.sqrt;\nimport static java.lang.Math.PI;\n\npublic class StaticImportTest\n{\n  public static void main(String&#x5B;] args)\n  {\n    System.out.println(&quot;Using System.out.println(...)&quot;);\n    out.println(&quot;Using out.println(...)&quot;);\n\n    out.println(&quot;The square root of 64 is &quot; + Math.sqrt(64));\n    out.println(&quot;The square root of 91 is &quot; + sqrt(91));\n\n    out.println(&quot;The value of PI is &quot; + PI);\n  }\n}\n<\/pre><\/div>\n\n\n<p>which, when run in a Java Virtual Machine supporting Java 5 or above, produces the following output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Using System.out.println(...)\nUsing out.println(...)\nThe square root of 64 is 8.0\nThe square root of 91 is 9.539392014169456\nThe value of PI is 3.141592653589793<\/strong><\/pre>\n\n\n\n<p>In a JVM supporting an earlier version of Java than 5, the program will produce compilation errors.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Automatic_import\"><\/span>Automatic import<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p><strong>Package\u00a0<code>java.lang<\/code><\/strong><\/p>\n\n\n\n<p>The following import statement is implicit in every Java program:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>import java.lang.*; \/\/ import all classes in java.lang<\/strong><\/pre>\n\n\n\n<p>so,&nbsp;<em><strong>you never have to add that particular import statement to your program source<\/strong><\/em>.<\/p>\n\n\n\n<p>The following&nbsp;<strong>public&nbsp;<\/strong>types are defined in&nbsp;<strong>java.lang&nbsp;<\/strong>(this is not a complete list, and is subject to augmentation in every release of Java):<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>AbstractMethodError <\/strong><br><strong>ArithmeticException <\/strong><br><strong>ArrayStoreException <\/strong><br><strong>Boolean <\/strong><br><strong>Character <\/strong><br><strong>Class <\/strong><br><strong>ClassCastException <\/strong><br><strong>ClassCircularityError <\/strong><br><strong>ClassFormatError <\/strong><br><strong>ClassLoader <\/strong><br><strong>ClassNotFoundException <\/strong><br><strong>CloneNotSupportedException <\/strong><br><strong>Cloneable <\/strong><br><strong>Compiler <\/strong><br><strong>Double <\/strong><br><strong>Error <\/strong><br><strong>Exception <\/strong><br><strong>ExceptionInInitializerError <\/strong><br><strong>Float <\/strong><br><strong>IllegalAccessError <\/strong><br><strong>IllegalAccessException <\/strong><br><strong>IllegalArgumentException<\/strong><br><strong>IllegalMonitorStateException<\/strong><br><strong>IllegalThreadStateException <\/strong><br><strong>IncompatibleClassChangeError <\/strong><br><strong>IndexOutOfBoundsException <\/strong><br><strong>InstantiationError <\/strong><br><strong>InstantiationException <\/strong><br><strong>Integer <\/strong><br><strong>InternalError <\/strong><br><strong>InterruptedException<\/strong><\/td><td><strong>LinkageError <\/strong><br><strong>Long <\/strong><br><strong>Math <\/strong><br><strong>NegativeArraySizeException <\/strong><br><strong>NoClassDefFoundError <\/strong><br><strong>NoSuchFieldError<\/strong><br><strong>NoSuchMethodError<\/strong><br><strong>NullPointerException <\/strong><br><strong>Number <\/strong><br><strong>NumberFormatException <\/strong><br><strong>Object <\/strong><br><strong>OutOfMemoryError <\/strong><br><strong>Process <\/strong><br><strong>Runnable <\/strong><br><strong>Runtime <\/strong><br><strong>RuntimeException <\/strong><br><strong>SecurityException <\/strong><br><strong>SecurityManager <\/strong><br><strong>StackOverflowError <\/strong><br><strong>String <\/strong><br><strong>StringBuffer <\/strong><br><strong>System <\/strong><br><strong>Thread <\/strong><br><strong>ThreadDeath <\/strong><br><strong>ThreadGroup <\/strong><br><strong>Throwable <\/strong><br><strong>UnknownError <\/strong><br><strong>UnsatisfiedLinkError <\/strong><br><strong>VerifyError <\/strong><br><strong>VirtualMachineError<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Default_Package\"><\/span>The Default Package<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Another package that is implicitly and automatically imported in every Java program is the&nbsp;<em><strong>default package<\/strong><\/em>.&nbsp; The default package is the package which contains every Java class that does not have an explicit package definition.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong><u>Note:<\/u><\/strong>&nbsp;I strongly discourage the use of the default package for other than truly trivial (i.e. not intended for any serious application) Java code.<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Current_Package\"><\/span>The Current Package<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Finally, one more package which is automatically imported is the package specified for the current Java class.<\/p>\n\n\n\n<p>In other words, if you place your class&nbsp;<code>Fontleroy<\/code>&nbsp;into the&nbsp;<code>little.lord<\/code>&nbsp;package, then, when the Java compiler compiles your class, it implicitly does the following import:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>import little.lord.*;<\/strong><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_package_Statement\"><\/span>The package Statement<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>When you wish to create a class that is to belong to a named Java package, you must place a&nbsp;<em><strong>package statement<\/strong><\/em>&nbsp;as the first statement (i.e. first text other than comments and whitespace) in the class&#8217;s Java source code file.<\/p>\n\n\n\n<p>Here is a package statement:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>package COM.Aardvark.eng.gui;<\/strong><\/pre>\n\n\n\n<p>where the package name must be&nbsp;<em>fully qualified&nbsp;<\/em>&#8212; that is, it must include the top-level package name and all intervening sub-package names .<\/p>\n\n\n\n<p>A compilation unit that has no package statement is part of an&nbsp;<em><strong>unnamed package<\/strong><\/em>&nbsp;&#8212; the default package.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A Java system is required to support&nbsp;<em>at least one unnamed package<\/em><\/li><li>It may support more than one, but is&nbsp;<em>not required to do so<\/em>.<\/li><li>Unnamed packages are a convenience feature when developing simple or temporary development, or when just starting to learn Java.<\/li><li>Unnamed packages are strongly discouraged in real-world Java code.<\/li><\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong><em>Note:<\/em>&nbsp;From this point on, I will expect you to use&nbsp;<em>only<\/em>&nbsp;named packages in your Java code!<\/strong><\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Compilation_Units\"><\/span>Compilation Units<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>A Java&nbsp;<em>compilation unit<\/em>&nbsp;is defined in the&nbsp;<em><strong>Java Language Specification<\/strong><\/em>&nbsp;as:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em><strong>CompilationUnit :\n   PackageDeclaration<sub>opt<\/sub> ImportDeclarations<sub>opt<\/sub> TypeDeclarations<sub>opt<\/sub><\/strong><\/em><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><em><strong>TypeDeclarations :\n   TypeDeclaration\n   TypeDeclarations TypeDeclaration<\/strong><\/em><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><em><strong>TypeDeclaration :\n   ClassDeclaration\n   InterfaceDeclaration<\/strong><\/em><\/pre>\n\n\n\n<p>Notice the subscript &#8220;opt&#8221;, which means optional.<\/p>\n\n\n\n<p>The above means that a Java compilation unit consists of:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Optionally, a package declaration, followed by<\/li><li>Optionally, a set of one or more import declarations, followed by<\/li><li>Optionally, a set of one or more type declarations, each type declaration may comprise:<ul><li>A class declaration, or&nbsp;<\/li><li>An interface declaration (more about interfaces, soon)<\/li><\/ul><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Host_Support_for_Packages\"><\/span>Host Support for Packages<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Each Java host determines:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>How packages, compilation units, and sub-packages are created and stored,<\/li><li>Which top-level package names are in scope in a particular compilation, and<\/li><li>Which packages are accessible<\/li><\/ul>\n\n\n\n<p>The packages may be stored:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>In a local file system, or:<\/li><li>In a distributed file system, or:<\/li><li>In some form of database<\/li><\/ul>\n\n\n\n<p>On Microsoft Windows and on Unix systems, they are typically stored in a directory hierarchy that matches the naming hierarchy. This typically means that a package is a&nbsp;<em>directory<\/em>&nbsp;containing&nbsp;<strong>.class<\/strong>&nbsp;files and\/or subdirectories. The&nbsp;<em>subdirectories<\/em>&nbsp;are&nbsp;<em>sub-packages<\/em>&nbsp;of that package.<\/p>\n\n\n\n<p><em><strong>Note:&nbsp;<\/strong><\/em>Remember:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Package and class names are&nbsp;<em><strong>case-sensitive!<\/strong><\/em><\/li><li>Somehow, the package directory must be accessible through the&nbsp;<code><strong>CLASSPATH<\/strong><\/code>&nbsp;environment variable.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Type_Definitions\"><\/span>Type Definitions<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Within a&nbsp;<em>compilation unit<\/em>&nbsp;you may have several&nbsp;<em><strong>TypeDeclaration<\/strong><\/em>s. However, note the following:From &#8220;The Java Language Specification&#8221;, by James Gosling, Bill Joy and Guy Steele, Addison-Wesley:<\/p>\n\n\n\n<p><em>&#8220;When Java packages are stored in the file system, the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as&nbsp;<strong>.java<\/strong>&nbsp;or&nbsp;<strong>.jav<\/strong>) if either of the following is true:<\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em>The type is referred to by code in other compilation units of the package in which the type is declared, or:<\/em><\/li><li><em>The type is declared public (and therefore is potentially accessible from code in other packages).<\/em><\/li><\/ul>\n\n\n\n<p><em>This restriction implies that there must be at most one such type per compilation unit. This restriction makes it easy for a Java compiler and Java Virtual Machine to find a named class within a package. For example, the code for a&nbsp;<strong>public<\/strong>&nbsp;type&nbsp;<strong>wet.sprocket.Toad<\/strong>&nbsp;would be found in a file&nbsp;<strong>Toad.java<\/strong>&nbsp;in the directory&nbsp;<strong>wet\/sprocket<\/strong>. The corresponding object code would be found in the file&nbsp;<code><strong>Toad.class<\/strong><\/code>&nbsp;in the same directory.<\/em><\/p>\n\n\n\n<p><em>When Java packages are stored in a database, the host system need not enforce such restrictions.<\/em><\/p>\n\n\n\n<p><em>In practice, many Java programmers choose to put each class or interface type in its own compilation unit, whether or not it is&nbsp;<strong>public<\/strong>, or is referred to by code in other compilation units.&#8221;<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Access_Control\"><\/span>Access Control<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Access_to_Package_Contents\"><\/span>Access to Package Contents<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>A package is accessible if the corresponding files and directories are accessible.<\/p>\n\n\n\n<p>All classes (and interfaces) in a package&nbsp;<em>are accessible to all other classes (and interfaces) in the same package<\/em>.<\/p>\n\n\n\n<p>A class declared&nbsp;<strong>public&nbsp;<\/strong>in one package&nbsp;<em>is accessible from within another package<\/em>. A non-<strong>public&nbsp;<\/strong>class&nbsp;<em>is&nbsp;<strong>not<\/strong>&nbsp;accessible from outside of its package<\/em>.<\/p>\n\n\n\n<p>Members of a class&nbsp;<em>are accessible from a different class within the same package, as long as they are not declared<\/em>&nbsp;<strong>private<\/strong>.<\/p>\n\n\n\n<p><strong>private<\/strong>&nbsp;members&nbsp;<em>are accessible only within their own class<\/em>.<\/p>\n\n\n\n<p>All members of a class&nbsp;<em>are accessible from within that class<\/em>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Visibility_Modifiers\"><\/span>Visibility Modifiers<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>The keywords&nbsp;<strong>public&nbsp;<\/strong>and&nbsp;<strong>private&nbsp;<\/strong>(and&nbsp;<strong>protected&nbsp;<\/strong>&#8212; later) are&nbsp;<em>visibility modifiers<\/em>, which have the following effect:<\/p>\n\n\n\n<p>A&nbsp;<strong>private&nbsp;<\/strong>member of a class&nbsp;<em>is visible only in methods defined within that class<\/em>.<\/p>\n\n\n\n<p>A&nbsp;<strong>public&nbsp;<\/strong>member of a class&nbsp;<em>is visible to all class methods.<\/em><\/p>\n\n\n\n<p>If a member is declared with&nbsp;<em>no visibility modifiers<\/em>, then it has&nbsp;<em>default package visibility<\/em>, and&nbsp;<em>is visible&nbsp;<strong>only<\/strong>&nbsp;within the class that defines it and within classes defined in the same package<\/em>. (This is analogous to the&nbsp;<em>friend<\/em>&nbsp;concept in C++ &#8212; all classes within a package are &#8216;friendly&#8217; to each other.)<\/p>\n\n\n\n<p>To summarize:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th><br><\/th><th><strong>Member<\/strong><\/th><th><strong>Visibility<\/strong><\/th><th> <\/th><\/tr><tr><th> <strong>Accessible to:<\/strong><\/th><th><strong>public<\/strong><\/th><th><strong>package<\/strong><\/th><th><strong>private<\/strong><\/th><\/tr><tr><td>Same class<\/td><td><strong>yes<\/strong><\/td><td><strong>yes<\/strong><\/td><td><strong>yes<\/strong><\/td><\/tr><tr><td>Class in same Package<\/td><td><strong>yes<\/strong><\/td><td><strong>yes<\/strong><\/td><td>no<\/td><\/tr><tr><td>Subclass in different package<\/td><td><strong>yes<\/strong><\/td><td>no<\/td><td>no<\/td><\/tr><tr><td>Non-subclass, different package<\/td><td><strong>yes<\/strong><\/td><td>no<\/td><td>no<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Here are some simple rules to help you choose which visibility modifiers to use, when:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Use&nbsp;<strong>public&nbsp;<\/strong>only for methods and constants that form part of the&nbsp;<em>public interface<\/em>&nbsp;(sometimes called the API) of the class.<\/li><li>Use the&nbsp;<em>default package visibility<\/em>&nbsp;for fields and methods that you want to be&nbsp;<em>hidden from outside of the package<\/em>, but which you want&nbsp;<em>cooperating (&#8216;friend&#8217;) classes within the package to have access to<\/em>.<\/li><li>Use&nbsp;<strong>private&nbsp;<\/strong>for fields and methods that are&nbsp;<em>only used inside the class and should be hidden from everywhere else<\/em>.&nbsp;<\/li><\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong><em>Note:<\/em><\/strong>&nbsp;Try to make&nbsp;<em>all class\/instance data<\/em> (fields)&nbsp;<strong>private<\/strong>, except for static final fields (i.e. class constants).<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Name Spaces and Reusability A major problem when developing reusable code is that of&nbsp;name conflicts. Vendor B might develop a library of classes, while Vendor M might develop another library of classes. Customer C wants to use both libraries, but discovers that a number of class names have been duplicated across the two libraries. What [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":34,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_eb_attr":"","_uag_custom_page_level_css":"","ocean_post_layout":"left-sidebar","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"ocs-course-topics-sidebar","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","footnotes":""},"class_list":["post-59","page","type-page","status-publish","hentry","entry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"ocean-thumb-m":false,"ocean-thumb-ml":false,"ocean-thumb-l":false},"uagb_author_info":{"display_name":"Bryan Higgs","author_link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/author\/bryan\/"},"uagb_comment_info":0,"uagb_excerpt":"Name Spaces and Reusability A major problem when developing reusable code is that of&nbsp;name conflicts. Vendor B might develop a library of classes, while Vendor M might develop another library of classes. Customer C wants to use both libraries, but discovers that a number of class names have been duplicated across the two libraries. What&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/59","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/comments?post=59"}],"version-history":[{"count":9,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/59\/revisions"}],"predecessor-version":[{"id":613,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/59\/revisions\/613"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/34"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/media?parent=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}