Method to define the packages in java programme, JAVA Programming

Assignment Help:

Q. Write the method to define the packages in java programme. Explain.

Ans. Package: When we work on a project we have to break our programme in several classes. To organize our classes we use packages. The package is both a naming and visibility control mechanism. Package is a collection of classes and interfaces which are interrelated. With the help of package, it is possible to give same names to more than one class provided; they are defined in different packages. A single package cannot contain two classes with same name. Programmes are organized as sets of packages. Each package has its own sets of sub packages, which helps to prevent name confections. A top level package is accessible outside the package is hierarchal. The members of a package are class and interface type and sub classes. A package in java is an encapsulation mechanism that can be used for to group related class, interface and sub packages. For small programmes, a package can be unnamed or have a simple name, but if code is to be widely distributed, unique package names should be chose. This can prevent to the conflicts that would otherwise occur if two development groups happened to pick the same package name and these packages were later to be used in a single programme. If we do specify a package for a java class then that class will be the part of the default package of java called as unnamed package. But this requires that every class must have a unique name to avoid collision.

How to define a package

To create a package, include a package command as the first statement in a java source file. Any class declared within that file will belong to the specified package. The package statement defines a name space in which classes are stored. If one omit the package statement, the class name are put into the default package, which has no name and is called as unnamed package. The package statement has the following syntax:

Package package _ name

Java uses file system directories to store packages. Remember that the case is significant, and directory name must match the package name exactly. More than one file can include the same package name exactly. More

Package p 1;      // package declaration

Class A                // class declaration

.................     // body of class A

.....................

}

Package is a keyword; p 1 is the package name in which class A is to be stored.

Example 1:

Package p 1;

Class example _ 1

{

Public static void main (string args [])

 {

System. out . print ln ("first package programme")

}                                              

} we have assumed that the current folder is (c:\java >)

Output

First package programme

Note: If we compile the above programme with the folder p 1 will be created automatically and the class file of the above programme will be saved in this folder. But if we do not compile with d option this it is our duty to create the p 1 folder and place the class file in that folder (.) directs the compiler the to create the p 1 folder in the current folder. We have created two files (Box. Java and box _ demo. Java ). The box _ demo. Java contains the main programme, so we will return with this class name.

Step 1

We have to create the java files in the c:\ java & box _ demo. Java)

Note 1: we can't run a java programme directly through class name if it is a part of a package. To run this programme we must specify the (pl. Java box _ demo)

If we run the above programme from any other folder than the current folder then it will not run. By default the java run time system uses the current run time directory as its starting point. Thus if our package is in the current directory, as its starting point or a subdirectory of the current directory, it will be found. But to run the above programme from any other folder we have to set the class path using the command SET CLASSPATH = % CLASSPATH %; C : TEST on command promote. CLASSPATH is a environment variable. We have to type this command every time when we start the computer for the first time. But if we we put this command in auto exec, bat file in the root directory then there is no need to type this command again & again. We can also run the programme without setting the CLASSPATH environment variable to do this type.


Related Discussions:- Method to define the packages in java programme

Design a bond class, In the lectures, we have written programs for bond pri...

In the lectures, we have written programs for bond pricing in discrete time. We have also talked about the theory for bond pricing and yields in continuous time. In this exercise,

What is java bytecode, What is java Bytecode Java programs (Source code...

What is java Bytecode Java programs (Source code) are compiled into a form known as Java bytecodes. Java compiler reads Java language source (.java) files, translates source

Use javascript create some basic client-side validation, The last exercise ...

The last exercise of each assignment will be continuous from assignment to assignment and will have the goal of building an e-store website by the end of the course. Date & Tim

Web application using javascript-xml and css, Journal of how you came to AU...

Journal of how you came to AUB Write a web application using JavaScript, XML and CSS that contains a journal of major events and places on your journey to Bond University. The

Program execution of boggle game application, In this assignment, you will ...

In this assignment, you will implement the game of Boggle, a word game in which players connect adjacent letters on a 4x4 board to create as many words as possible. This is based o

Develop a school java project, Develop a School java project Project Des...

Develop a School java project Project Description: Programming java, threads. Not so hard but requires knowledgeSkills required: Java

#title.swithcase, 2. Problem Description: A Java program needs to be writte...

2. Problem Description: A Java program needs to be written to display the result of following operations based input character. The table for input character with operations is : I

What is the function of network interface card, What is the function of Net...

What is the function of Network Interface Card? NIC is an I/O device which plugs within the computer and it enables it to communicate over a network.

What is bandwidth, What is bandwidth? In a general way Bandwidth is a c...

What is bandwidth? In a general way Bandwidth is a capacity of communication channel of carrying data.

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd