Home TechnologyProgrammingJava Subscribe to RSS

How Can I Use Packages In Java

Answer Question

2 Answers - Sort by: Date | Rating

    Packages provide an easy way for name space management. In the packages related classes and interfaces are grouped together. The classes and interfaces that provide similar functionality are packaged into one group. Every time when a package is created a new name space is created. Classes defined in a package at the same level can access the members of other classes. Packages can also be nested in other packages. Following are steps of creating and using the packages in java:

    Packages are to be declared before they can be used. The following statement is written at the start of every source file that is to be included in the package.package package_name;  
    Package declaration contains the keyword 'package' followed by the package name.All the source files are then placed in the directory whose name is the same as that of the package.

    Compile the source files to produce the class files for each type defined in the source code. You can place the .java files any where however the class files must be in a directory whose name is the same as that of the package.Then set the classpath variable as followsjavac –classpath "d\javasourcefiles\mypackage" mytype.java.Here "d\javasourcefiles\mypackage" is the path of the package and mytype.java is the source file.o use the classes defined in a package you need to import the specific class or the package in your program using the import statement like the one as follows:mport java.awt.*;This statement allows you to use all the types defined in the package awt.
    0 0

    Raaga 

    answered 3 years ago

      You simply import them first and can then just call the public methods within.
      0 0

      Scrut 

      answered 3 years ago

        More

           
           

          Ask a Question via Twitter

          Send a question to @askblurtit and we will publish it online and send you a reply everytime you receive an answer.

          Blurtit Store

          Get T-shirts, hoodies, caps and more at the Blurtit store

          Blurtit International