Today we will learn how to make package in java, but before making java package , first of all we should know about what is java package ,and why it needs.? Basically package are like folders which contain java class files.package are made when we want to use some set of code or values in another program.it decrease the size of program.when we make any project we make package so the program become readable and usable.when we make package we can import it any program.
Syntax to create package:
Also Read :
PHP Login Script With Remember me.
How to hide extension of html and php file.?
NOTE:The package creation should be at starting of any program.
Example: Addition of two numbers. Suppose we have two java files.First is taking the input from user and second is doing the addition of that numbers.Both are individual file so,we will join them using package.So,the advantage of package is that we can also make third class which done the subtraction of the same numbers used for addition.
In package main is create in one class and from that class we call other class by making object of that class.When we compile package file the the file which contains main() compile at last otherwise it will generate error.We we run package it is run through the name of class which contain main().
1) package is addition and the class which contain main and a object of second class is create and call add method and pass value of a and b.
2) package is addition and which is called from main class
HOW TO COMPILE AND RUN PACKAGE:
Note: Firstly went in the drive and folder where you have saved your jave file.Whether it is in inside bin and any other folder. In my case my java files are inside e:\try so,on command prompt type:-first went inside drive by using drive name followed by colon and press enter after that cd space folder name.if you have one more folder inside that the do same cd space folder name.
Now we are in the folder where we have our java file.Lets compile our files.
avac –d “<“path of folder where you want to create your package”>” “<“file-name.java”>”
Note:- d is used for destination of package.
Example: We have compile our both files. two.java and one.java.one.java is coimpile after two.java because it contains main method.
Now we have run it,we run package by its main class name.