follow

Friday, 26 July 2013

Package : JAVA Programming by "Gopal Krishna"

{

  1.  एक फोल्डर बनाना है जिसका नाम वही रखना है जो पैकेज का नाम है |
  2. उस फोल्डर में आपको एक प्रोग्राम लिखना है जिसमे आप कोई से भी फंक्शन हो सकते हैं लेकिन में मेन मेथड नहीं होगा |
  3. मैंने जो प्रोग्राम बनाया है उसमे मैंने डीयर नाम का एक पैकेज बनाया है उसके अन्दर दो जावा फ़ाइल लिखी है  तथा कम्पाइल की हैं | कम्पाइल करने पर उसमे क्लास फ़ाइल आ जायेगी | जिसका प्रयोग हम किसी और प्रोग्राम में करेंगे | ध्यान रहे कि यदि आपके पैकेज का नाम dear है तो आपको पैकेज ( फोल्डर )के अन्दर रखी जावा फाइलों की पहली लाइन package dear; होना चाहिए |
  4. फिर हमने पैकेज ( जो कि एक प्रोग्राम का फोल्डर है ) के साथ ही एक और फ़ाइल रखी जिसमे हम इन क्लास फाइल्स का प्रयोग करेंगे | 
  5.  import dear.*;  ये हम अपने उस प्रोग्राम के ऊपर पहली लाइन में लिखेंगे जिसमें हम पैकेज की क्लास फ़ाइल को  यूज करना चाहते हैं |

}

make a folder and rename it as dear 

now save one file as xyz.java :

package dear;

public class xyz

{
public int a(int i)
{
i=i+1;
System.out.println("The valueof i ->"+i);
return 0;
}
public void disp()
{
System.out.println("i am store in package dear");
}

public void d()
{
System.out.println("i am also store in package dear");
}
}


Save as gkg.java in folder dear :


package dear;

public class gkg
{

public int ak(int i)
{
i=i+2;
System.out.println("The valueof i ->"+i);
return 0;
}

public void dispk()
{
System.out.println("i am store in package dear");
}


public void dk()
{
System.out.println("i am also store in package dear");
}
}


Save as krishna.java parlel to folder dear

import dear.*;

class krishna
{
public static void main(String arg[])
{
xyz j=new xyz();
gkg k=new gkg();
j.d();
j.disp();
j.a(1);

k.dk();
k.dispk();
k.ak(1);


}
}




Code is ready compile and use package in class krishna .. :) 



Hide class in package: If we wants to hide any class then not use public in front of that .



NOTE : we call with the package name if two classes having same name but diffrent function.

abc.xyz.x=new xyz();

No comments:

Post a Comment