{"id":3415,"date":"2017-07-24T10:45:18","date_gmt":"2017-07-24T10:45:18","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=3415"},"modified":"2024-12-14T04:53:36","modified_gmt":"2024-12-14T04:53:36","slug":"how-to-import-a-package-in-java","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/how-to-import-a-package-in-java\/","title":{"rendered":"How to Import a Package in Java"},"content":{"rendered":"<h1>What is a Package<\/h1>\n<p>In java, we can group a number of classes into a single unit known as packages which also act as a container for a group of classes. Packages resolve name conflicts in the class names. Different packages can contain the classes with the same names. We can import a package using the import statement.<\/p>\n<p>Example:<\/p>\n<p>There are several inbuilt java packages.<\/p>\n<p>java.lang.*;<br \/>\njava.io.*;<br \/>\njava.net.*;<br \/>\njava.util.*;<\/p>\n<h3>Define a Package<\/h3>\n<p>A programmer can create his own package to group related classes of the development effort.<\/p>\n<p><strong>Syntax :<\/strong><\/p>\n<p><strong>package name;<\/strong><\/p>\n<p>A class that defines inside a package &#8220;com.testingdocs.tutorial&#8221; should be placed inside the directory i.e &#8220;com\\testingdocs\\tutorial&#8221;.<\/p>\n<p>In this way, a class hierarchy can be built. A hierarchy of packages can be created to better organize the classes in the application development or automation framework development.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3700\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/java-package-example.png\" alt=\"\" width=\"1696\" height=\"985\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/java-package-example.png 1696w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/java-package-example-300x174.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/java-package-example-1024x595.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/java-package-example-768x446.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/java-package-example-1536x892.png 1536w\" sizes=\"auto, (max-width: 1696px) 100vw, 1696px\" \/><\/p>\n<h3>Creating a Package<\/h3>\n<p>Creating a package in Java is simply creating a package command followed by the name of the package as the first name in the Java source file.<\/p>\n<p><strong>Example<\/strong><\/p>\n<p>package com.testingdocs.tutorial;<\/p>\n<h3>Import a Package<\/h3>\n<p>Example:<\/p>\n<p>package com.testingdocs.tutorial;<\/p>\n<p>public class Test<\/p>\n<p>{<\/p>\n<p>String testName;<\/p>\n<p>String testID;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3705\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Java-Package-and-Class.png\" alt=\"Java Package and Class\" width=\"1725\" height=\"875\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Java-Package-and-Class.png 1725w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Java-Package-and-Class-300x152.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Java-Package-and-Class-1024x519.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Java-Package-and-Class-768x390.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Java-Package-and-Class-1536x779.png 1536w\" sizes=\"auto, (max-width: 1725px) 100vw, 1725px\" \/><\/p>\n<p>In the above example using the statement will create a package with folder com.testingdocs.tutorial in the project directory. We can import this class in another class using the import statement.<\/p>\n<p>For example: Importing a class in a package<\/p>\n<p><strong>import com.testingdocs.tutorial.Test;<\/strong><\/p>\n<p>Importing the whole package using the star notation:<\/p>\n<p><strong>import com.testingdocs.tutorial.*;<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3701\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Import-package-vs-Class.png\" alt=\"Import package vs Class\" width=\"1729\" height=\"1028\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Import-package-vs-Class.png 1729w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Import-package-vs-Class-300x178.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Import-package-vs-Class-1024x609.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Import-package-vs-Class-768x457.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Import-package-vs-Class-1536x913.png 1536w\" sizes=\"auto, (max-width: 1729px) 100vw, 1729px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is a Package In java, we can group a number of classes into a single unit known as packages which also act as a container for a group of classes. Packages resolve name conflicts in the class names. Different packages can contain the classes with the same names. We can import a package using [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-3415","post","type-post","status-publish","format-standard","hentry","category-java","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3415","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=3415"}],"version-history":[{"count":9,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3415\/revisions"}],"predecessor-version":[{"id":26436,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3415\/revisions\/26436"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=3415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=3415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=3415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}