Monday, 7 September 2015

Java Tutorial : Java package access


Click here to watch in Youtube :
https://www.youtube.com/watch?v=mSdCLh8wncU&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Java package access
Java Tutorial : Java package access
1.import packagename.*;

A.java
package mypack;

public class A
{

    public void message()
    {
        System.out.println("Class A messgage has been called.....");

    }

}
B.java
package util;

/**
 * If you use packagename.* then all the classes and interfaces of this package
 * will be accessible but not sub packages.
 *
 * The import keyword is used to make the classes and interface of
 * another package accessible to the current package.
 */
import mypack.*;

public class B
{

    public static void main(String[] args)
    {
        A a = new A();
        a.message();
    }

}
Output
Class A messgage has been called.....

2.import packagename.classname;

A.java
package mypack;

public class A
{

    public void message()
    {
        System.out.println("Class A messgage has been called.....");

    }

}
B.java
package util;

/**
 * If you import packagename.classname then only declared class of 
 * this package will be accessible.
 */
import mypack.A;

public class B
{

    public static void main(String[] args)
    {
        A a = new A();
        a.message();
    }

}
Output
Class A messgage has been called....

3.Fully qualified name.

A.java
package mypack;

public class A
{

    public void message()
    {
        System.out.println("mypack Class A messgage method has been called.....");

    }

}
A.java
package util;

public class A
{

    public static void main(String[] args)
    {
        /*
         * If you use fully qualified name then only declared class of this
         * package will be accessible. Now there is no need to import. But you
         * need to use fully qualified name every time when you are accessing
         * the class or interface.
         * 
         * It is generally used when two packages have same class name e.g.
         * java.util and java.sql packages contain Date class.
         */
        mypack.A a = new mypack.A();
        a.message();
    }

}
Output
mypack Class A messgage method has been called.....
To Download PackageAccessApp Project Click the below link
https://sites.google.com/site/javaee4321/java/PackageAccessApp.zip?attredirects=0&d=1

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment