Skip to main content

how to create java package and use it

java creating simple package containing class and display method in another program accessing that package using import keyword video tutorial





A package is a collection of related classes. it helps improve re-usability.

It helps Organize your classes into a folder structure and make it easy to locate and use them.
Syntax used to create package is 
package;

 Step 1) Consider the following code,

·       Save as demo.java
·       Compile using javac demo.java
·       Re comple
·       javac -d . demo.java
·       Packge name folder created
·       Java p1.c1 fully qulified name is used to run mehtod 


package p1;

class c1{
public void m1()
{
System.out.println("pakge class ");
}

public static void main(String s[])
{
c1 obj=new c1();
obj.m1();
}

}



lets take example in above video follow steps in video copy below code 


Save as 
Emp.java

package prakash;
public class Emp
{
 public void display()
 {
  System.out.println("hi its method display of pkg Prakash");
 }

}

1) Sve program in C Drive
2)Comple using Javac Emp.java
3) Re comple using  Javac -d . Emp.java
after 2nd compilation folder prakash created on D drive 


Creat another program Save and Comple using
Javac PkgDemo.java 
Run using java PkgDemo.



save as 
PkgDemo.java 

import prakash.*;

class PkgDemo
{
public static void main(String s[] )
{
prakash.Emp obj=new prakash.Emp();
obj.display();
}


}


Comments

Labels

Show more

Popular posts from this blog

Calculator Program using jFrame

Calculator Program using jFrame java swing calculator example java jframe calculator source code java code for simple calculator using gui how to make a calculator in advance java using JFrame simple calculator program in java using frame simple calculator program in java source code write a code to create simple calculator using swing component import javax.swing.*; import java.awt.event.*; public class TextFieldExample extends JFrame implements ActionListener{     JTextField tf1,tf2,tf3;     JButton b1,b2; //declaration of control     TextFieldExample(){         JFrame f= new JFrame("Button click demo"); //         tf1=new JTextField();         tf1.setBounds(50,50,150,20);         tf2=new JTextField();         tf2.setBounds(50,100,150,20);         tf3=new JTextField();       ...

Java Program to delete record using JFrame and Prepaired Statement

Java Program to delete record using JFrame and Prepaired Statement Here is table Design  Create table demo using two column as id  and Dname , id is primary key  Program source code  +import javax.swing.*;   import java.awt.event.*;   import java.sql.*; public class FrameDelete extends JFrame implements ActionListener{       JTextField tf1,tf2,tf3;       JButton b1,b2;   JLabel l1,l2; Connection con;    PreparedStatement psmt;   int result; //declaration of control    FrameDelete(){           JFrame f= new JFrame("InsertDemo");   // l1=new JLabel("Enter RollNo"); l1.setBounds(150,10,150,50);         tf1=new JTextField();           tf1.setBounds(150,50,150,20);   tf3=new JTextField();           tf3.setBounds(1...

java insert using JFrame and Prepaired Statement

Java Program to insert record using JFrame and Prepaired Statement Here is table Design  Create table demo using two column as id  and Dname , id is primary key  Program source code  import javax.swing.*;   import java.awt.event.*;   import java.sql.*; public class FrameInsert extends JFrame implements ActionListener{       JTextField tf1,tf2,tf3;       JButton b1,b2;   JLabel l1,l2; Connection con;    PreparedStatement psmt;   int result; //declaration of control    FrameInsert(){           JFrame f= new JFrame("InsertDemo");   // l1=new JLabel("Enter RollNo"); l1.setBounds(150,10,150,50);         tf1=new JTextField();           tf1.setBounds(150,50,150,20);   l2=new JLabel("Enter Name"); l2.setBounds(150,60,150,5...