Ads

Saturday, 4 November 2017

User Defined Package in Java


User-defined package


package keyword is used to create the user define the package.


syntax


package demo
class demo
{
    //methods;
}

Example-1

package pack
class A
{
   void dis(String msg)
   {
      System.out.pritnln("String"+msg");
    } 
}
class B
{
    public static void main(String args[])

    {
          A obj=new A();

          obj.dis("JAVAXPERT");
    }
}

 Example-2


//user-defined package.
File-1: Package file

package pkg   <-- Folder name

package pkg
{
    public void dis(string s)

    {
        System.out.println("Package call"+s);

     }
}

File-2 This file save in another folder.

import pkg.*;
class pkg2
{
    public static void main(String args[])

   {
      A obje=new A();

      String str=args[0];
      obj.dis(str);
    }
 }

Post a Comment

Click Here