java entertainment with package conditions
DESCRIPTION
package conditionshttps://www.facebook.com/javaentertainment?ref_type=bookmarkTRANSCRIPT
-
5/23/2018 java entertainment with Package conditions.
1/8
$ ENTERTAINMENT WITH PACKAGE $
[GOLDEN TRUTH 1] Two different folders having the same package with a same class.
{That is , if we set the classpath of two different folders having the same package with a same class
in my program/application then which one package will come in my program.}
We have created two different folders c:\f1 and c:\f3 both having the same class Temp1 containing
the same package making command p1. Now our task is to access the class temp1 of package p1
from another drive\dir\folder say from F:\f2 directory.
F:\f2 containing the class Temp2 with package making command p2 & import command of p1
package.
C:\f1 has Temp1.java
package p1;public class Temp1
{
public void show () {System.out.println ("package p1 from folder f1") ;}
public static void main (String s [])
{
Temp1 t1 = new Temp1 ();
t1.show ();
}
}
C:\f3 has Temp1.java
package p1;
public class Temp1
{
public void show () {System.out.println ("package p1 from folder f3") ;}
public static void main (String s [])
{
Temp1 t1 = new Temp1 ();
t1.show ();
}
}
F:\f2 has Temp2.java
package p2;
import p1.*;
public class Temp2
{
public static void main (String s [])
{
new Temp1 ().show ();
}}
-
5/23/2018 java entertainment with Package conditions.
2/8
Dos command prompt work:
C:\f1>javac -d . *.java // compiler will create Temp1.class inside package p1
C:\f1>cd..
C:\>cd f3
C:\f3>javac -d . *.java // compiler will create Temp1.class inside package p1
C:\f3> set classpath = c:\f1;c:\f3;
C:\f3>cd\
C:\>f:
F:\>cd f2
F:\f2>javac -d . *.java
// compiler go to the classpath it will see two package with same name in f1 & f3 ,but he use first
folder f1 to import package p1 in class Temp2 to create Temp2.class inside package p2
F:\f2>java p2.Temp2
Package p1 from folder f1
F:\f2> set classpath = c:\f3;c:\f1;F:\f2>javac -d. *.java
F:\f2>java p2.Temp2
Package p1 from folder f3
[GOLDEN TRUTH 2] Two different folders having the two different package, but with a same class.
{That is , if we set the classpath of two different folders having the different package with a same
class in my program/application then what will be happen. Means which class will come}
C:\f1 has Temp1.java
package p1;
public class Temp1
{
public void show () {System.out.println ("package p1 from folder C:\ f1") ;}
public static void main (String s [])
{
Temp1 t1 = new Temp1 ();
t1.show ();
}
}C:\f3 has Temp1.java
package p3;
public class Temp1
{
public void show () {System.out.println ("package p3 from folder C:\ f3") ;}
public static void main (String s [])
{
Temp1 t1 = new Temp1 ();
t1.show ();
}
}
-
5/23/2018 java entertainment with Package conditions.
3/8
F:\f2 has Temp2.java
package p2;
import p1.*;
import p3.*;
public class Temp2
{
public static void main (String s [])
{
new Temp1 ().show ();
}
}
Dos command prompt work:
C:\f1>javac -d . *.java // compiler will create Temp1.class inside package p1
C:\f1>cd..
C:\>cd f3C:\f3>javac -d . *.java // compiler will create Temp1.class inside package p3
C:\f3> set classpath = c:\f1;c:\f3;
C:\f3>cd\
C:\>f:
F:\>cd f2
F:\f2>javac -d . *.java
// compiler will generate ambiguous error (reference to Temp1 is ambiguous)
// In this case, we have to qualified the class Temp2 with package name
// change the F:\f2 \ Temp2.java
public static void main (String s [])
{
new p1.Temp1 ().show ();
}
F:\f2>javac -d . *.java
F:\f2>java p2.Temp2
Package p1 from folder C:\ f1
public static void main (String s [])
{
new p3.Temp1 ().show ();}
F:\f2>javac -d . *.java
F:\f2>java p2.Temp2
Package p3 from folder C:\ f3
-
5/23/2018 java entertainment with Package conditions.
4/8
[GOLDEN TRUTH 3] Two different folder having a same package, but with a different classes.
{That is , if we set the classpath of two different folders having a same package with different class in
my program/application then what will be happen. Means which classes will come}
C:\f1 has Temp1.java
package p1;
public class Temp1
{
public void show () {System.out.println ("package p1 from folder C:\ f1") ;}
public static void main (String s [])
{
Temp1 t1 = new Temp1 ();
t1.show ();
}}
C:\f3 has Temp3.java
package p1;
public class Temp3
{
public void show () {System.out.println ("package p1 from folder C:\ f3") ;}
public static void main (String s [])
{
Temp3 t3 = new Temp3 ();
t3.show ();
}
}
F:\f2 has Temp2.java
package p2;
import p1.*;
public class Temp2
{
public static void main (String s [])
{new Temp1 ().show ();
new Temp3 ().show ();
}
}
Dos command prompt work:
C:\f1>javac -d . *.java // compiler will create Temp1.class inside package p1
C:\f1>cd..
C:\>cd f3
C:\f3>javac -d . *.java // compiler will create Temp1.class inside package p3
C:\f3> set classpath = c:\f1;c:\f3;C:\f3>cd\
-
5/23/2018 java entertainment with Package conditions.
5/8
C:\>f:
F:\>cd f2
F:\f2>javac -d . *.java
F:\f2>java p2.Temp2
Package p1 from folder C:\ f1
Package p1 from folder C:\ f3
[GOLDEN TRUTH 4] Using the class of one package into the class of default package. In this case we
have to never keep any java file parallel to any package
C:\f1 has Temp1.java
package p1;
public class Temp1
{
public void show () {System.out.println ("package p1 from folder C:\ f1") ;}
public static void main (String s [])
{
Temp1 t1 = new Temp1 ();
t1.show ();
}
}
F:\f2 has Temp2.java
import p1.*;
public class Temp2{
public static void main (String s [])
{
new Temp1 ().show ();
}
}
Dos command prompt work:
C:\f1>javac -d . *.java // compiler will create Temp1.class inside package p1
C:\f1> set classpath = c:\f1;
F:\f2>javac Temp2.java
// error: cant access Temp1
Hint: remove source file from parallel to package or place source file in any sub dir/subfolder.
F:\f2>javac Temp2.java
F:\f2>java Temp2
Package p1 from folder C:\ f1
==================================================================================
-
5/23/2018 java entertainment with Package conditions.
6/8
[GOLDEN TRUTH 5] Using the one class into another class which is parallel to it, but another class is
also having a first class from the another package. In this case, priority always go to (local) parallel
class.
i.e. for eg. Using Temp1 into Temp2; Temp2 is parallel to Temp1 & also Temp2 import Temp1 from
another package.
C:\f1 has Temp1.java
package p1;
public class Temp1
{
public void show () {System.out.println ("package p1 of Temp1 from folder C:\ f1") ;}
public static void main (String s [])
{Temp1 t1 = new Temp1 ();
t1.show ();
}
}
F:\f2 has Temp1.java
package p2;
public class Temp1
{
public void show () {System.out.println ("package p2 of Temp1 from folder F:\ f2") ;}
public static void main (String s [])
{
Temp1 t1 = new Temp1 ();
t1.show ();
}
}
F:\f2 has Temp2.java
package p2;
import p1.*;
public class Temp2{
public static void main (String s [])
{
new Temp1 ().show ();
}
}
Dos command prompt work:
C:\f1>javac -d . *.java // compiler will create Temp1.class inside package p1
C:\f1> set classpath = c:\f1;
F:\f2>javac -d . *.javaF:\f2>java p2.Temp2
-
5/23/2018 java entertainment with Package conditions.
7/8
Package p2 of Temp1 from folder F:\ f2
[GOLDEN TRUTH 6] Using the class of one package into the class of another package without setting
the classpath.
C:\f1 has Temp1.java
package p1;
public class Temp1
{
public void show () {System.out.println ("package p1 from folder C:\ f1") ;}
public static void main (String s [])
{
Temp1 t1 = new Temp1 ();
t1.show ();
}
}
F:\f2 has Temp2.java
Package p2;
import p1.*;
public class Temp2
{
public static void main (String s [])
{
new Temp1 ().show ();}
}
Dos command prompt work:
C:\f1>javac -d . *.java
F:\f2> javac -classpath C:\f1; -d . *.java \\ F:\f2>javac -cp C:\f1; -d . *.java
F:\f2> java -classpath C:\f1;F:\f2; p2.Temp2 \\ F:\f2>java -cp C:\f1;F:\f2 ; p2.Temp2
Package p1 from folder C:\ f1
==================================================================================
$ ENTERTAINMENT WITH PACKAGE $
[GOLDEN TRUTH 7] Setting the classpath of any package via programs.C:> set classpath = F:\bhageria;
class SetClasspath
{
public static void main(String s[])
{
String cp = System.getProperty("java.class.path");
System.out.println(cp);
System.setProperty("java.class.path","c:\\f1");
String cp1 = System.getProperty("java.class.path");
System.out.println(cp1);
-
5/23/2018 java entertainment with Package conditions.
8/8
}
}
Output:
F:\bhageria
C:\\f1
[AMAZING GOLDEN TRUTH 8] Using the class of default package outside this package.
{NOTE} Only default package class can be accessed outside the package via setting the classpath
only with default package, it will not happen in case of any package .
Default -default = allowed
Default --> package = not allowed
C:\f1 has Temp1.java
public class Temp1
{
public void show () {System.out.println ("package p1 from folder C:\ f1") ;}
public static void main (String s [])
{
Temp1 t1 = new Temp1 ();
t1.show ();
}}
F:\f2 has Temp2.java
public class Temp2
{
public static void main (String s [])
{
new Temp1 ().show ();
}
}
C:\f1>javac Temp1.java
C:\f1>set classpath = C:\f1;
F:\f2> javac Temp2.java
F:\f2> java Temp2
Package p1 from folder C:\ f1