{"id":2704,"date":"2022-08-30T15:27:05","date_gmt":"2022-08-30T15:27:05","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/08\/package-in-java-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:27:05","modified_gmt":"2022-08-30T15:27:05","slug":"package-in-java-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/package-in-java-collection-of-common-programming-errors\/","title":{"rendered":"Package in Java-Collection of common programming errors"},"content":{"rendered":"<p>As I&#8217;m not a fan of these other answers, I&#8217;ll write my own.<\/p>\n<p><strong>Real World Examples:<\/strong><\/p>\n<p>Think of a &#8220;package&#8221; as an easy way for a java class to reference another.<\/p>\n<p>Let&#8217;s say I have this big box in my attic. I have a calculator, compass, protractor, etc. I can label this box <code>MathTools<\/code>.<\/p>\n<p>Another example would be taking all your pictures and putting them in the <code>Pictures<\/code> folder in your documents. From there, you could split them into <code>Spring Break 2009<\/code> or <code>[Insert Name Here]'s Party<\/code>.<\/p>\n<p>How does this relate to Java? Well, let&#8217;s look at the <code>java.util<\/code> package (you can reference this with <code>import java.util.*;<\/code>. You have ArrayLists, Strings, Random, etc. which are used in most Java programs (common &#8220;utilities&#8221;, if you prefer). There are all neatly organized into the same package, so that programmers can easily reference them (<code>import java.util.*;<\/code>).<\/p>\n<p><strong>Easy Application:<\/strong><\/p>\n<p>Let&#8217;s assume that we can find all the files to a small dice simulator in <code>C:\/Program Files\/Java Project\/my\/proj\/<\/code> (it&#8217;s likely that this file doesn&#8217;t exist on your computer, but just pretend for a moment).<\/p>\n<p>You have 3 files: <code>Main.java<\/code>, <code>Dice.java<\/code>, and <code>DiceRoller.java<\/code>. All of which are shown below:<\/p>\n<p>.<\/p>\n<p>&#8220;C:\/ProgramFiles\/Java Project\/my\/proj\/main\/<code>Main.java<\/code>&#8220;:<\/p>\n<pre><code>package my.proj.main;\n\nimport my.proj.sims.Dice;\n\npublic class Main\n{\n    public static void main(String[] args)\n    {\n        DiceRoller roller = new DiceRoller();\n        roller.rollAndShow(4);\n    }\n}\n<\/code><\/pre>\n<p>&#8220;C:\/ProgramFiles\/Java Project\/my\/proj\/sims\/<code>Dice.java<\/code>&#8220;:<\/p>\n<pre><code>package my.proj.sims;\n\nimport java.util.Random; \/\/ I used the Random class, but you can also use the Math class if you prefer (java.lang.Math)\n\npublic class Dice\n{\n    public Dice()\n    {\n    }\n\n    public int roll()\n    {\n        Random rand = new Random();    \n        return rand.nextInt(6) + 1; \/\/ Rolls a random number 1-6\n    }\n}\n<\/code><\/pre>\n<p>&#8220;C:\/ProgramFiles\/Java Project\/my\/proj\/sims\/<code>DiceRoller.java<\/code>&#8220;:<\/p>\n<pre><code>package my.proj.sims;\n\npublic class DiceRoller\n{ \n    public DiceRoller ()\n    {\n    }\n\n    \/\/ Rolls and prints the result of 'n' number of rolls\n    public void rollAndShow(int n)\n    {\n        Dice dice = new Dice();\n\n        for (int i = 0; i &lt; n; i++)\n        {\n            System.out.println(dice.roll()); \/\/ You should never use S.o.p in a method - it's bad practice, but it's easier this way if you don't yet understand the concept of objects\n        }\n    }\n}\n<\/code><\/pre>\n<p>.<\/p>\n<p>Things to notice:<\/p>\n<ul>\n<li><code>Main.java<\/code> is packaged into <code>my.proj.main<\/code><\/li>\n<li><code>Dice.java<\/code> is packaged into <code>my.proj.sims<\/code><\/li>\n<li><code>Main.java<\/code> needs to import <code>my.proj.sims.Dice<\/code> in order to create a <code>Dice<\/code> object and use its methods because it&#8217;s in a different package from <code>Dice.java<\/code>.<\/li>\n<li><code>DiceRoller.java<\/code> does not need to import <code>my.proj.sims.Dice<\/code> because it is in the same package as <code>Dice.java<\/code> and the compiler will automatically associate the two.<\/li>\n<\/ul>\n<p>.<\/p>\n<p><code>Import<\/code> is a command to load the functionality of a class into the current file. Look at <code>Dice.java<\/code>, for example. In order for it to create a <code>Random<\/code> object, which has the method <code>nextInt()<\/code>, it needs to import the Random class from the <code>java.util.*<\/code> package.<\/p>\n<p>.<\/p>\n<p>You might notice that some people would prefer to use <code>java.util.*<\/code> instead of <code>java.util.Random<\/code>, <code>java.util.ArrayList<\/code>, etc. What the <code>*<\/code> essentially means is any class within <code>java.util<\/code>. Running <code>import java.util.*<\/code> will import the Random, String, ArrayList, etc. classes.<\/p>\n<p>.<\/p>\n<p>Hope this clears things up. Please consider upvoting if this has helped you \ud83d\ude42<\/p>\n<p id=\"rop\"><small>Originally posted 2014-02-08 02:15:49. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>As I&#8217;m not a fan of these other answers, I&#8217;ll write my own. Real World Examples: Think of a &#8220;package&#8221; as an easy way for a java class to reference another. Let&#8217;s say I have this big box in my attic. I have a calculator, compass, protractor, etc. I can label this box MathTools. Another [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2704","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2704","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2704"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2704\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}