oqads

 photo adv_zpsn5opiawq.gif

Thursday, 26 January 2017

How to make package in java.?

HOW TO MAKE PACKAGE IN JAVA.?


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 porject we make package so the program become readable and usable.when we make package we can import it any program.


shortlearner.com



shortlearner.com



Syntax to create package:

package <package-name>;

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().

Code:
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.

package addition;
import java.util.Scanner;
class one
{
          public static void main(String args[])
          {
Scanner sc=new Scanner(System.in);
System.out.println("enter first no.");
int a=sc.nextInt();
System.out.println("enter second no.");
int b=sc.nextInt();
//object of another class is created which are in same package
two t=new two();
//calling of add method of another class
t.add(a,b);
}
}
         

 2) package is addition and which is called from main class

package addition;
class two
{
//this method is called from main class
public void add(int a,int b)
{
int c=a+b;
System.out.println("The addition of two no. is: "+c);
}
}


 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.see below:-


Now we are in the folder where we have our java file.Lets compile our files.

Syntax:

javac  –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.

Syntax:

java <package-name>.<class-name>

Example:-



IF YOU HAVE ANY DOUBT OR QUERY ABOUT THIS POST, YOU JUST ASK  YOUR  QUERY  IN COMMENT BOX…
YOU CAN ALSO ASK  QUERIES ON WHATSAPP ,JUST TYPE “MY QUERY” <QUERY> AND SEND US ON OUR WHATSAPP NUMBER +917415587271
 photo images 14_zpsaabtcc5x.jpg

click there

want to online earning click me