• Post Reply Bookmark Topic Watch Topic
  • New Topic

With respect to accessing static content, difference between custom package and java system package  RSS feed

 
karunakar theru
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q) With respect to accessing static content, what is the difference between user defined package and java system package (say java.lang etc)  I'm preparing for ocjp6. using 1.6.26 java version
my java program has a package named "pack" in PackageTest.java



javac -d . PackageTest.java
created PackageTest.class file in pack folder
now accessing static contents of PackageTest class from another java
program (TestStaticContents.java) as below



javac TestStaticContent.java 
displaying Compilation Error:
TestStaticContents.java: cannot access PackageTest bad class file: .\PackageTest.java

If i try accessing static contents of Math class from my java program
its not displaying any compilation error i.e




javac TestMathStaticContents.java
No Compilation Error, and PI value
is printed as expected
-------------------
Why this behavior is different compared to User defined package

 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 4114
34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try moving your PackageTest.java file away from the directory in which you have TestStaticContents.java while compiling TestStaticContents.java.
Or compile both the files together using -d  . option.
javac -d . *.java
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
karunakar theru wrote:Why this behavior is different compared to User defined package

That's very simple: classes from the java.lang package are imported by default. Although the import you are using is wrong to import the Math class, it still works because you simply don't need to add imports for classes in the java.lang package. So this class will compile and run successfully tooBut this only applies to classes in the java.lang package, not to user-defined packages nor to other packages from the JDK.

karunakar theru wrote:javac TestStaticContent.java
displaying Compilation Error:
TestStaticContents.java: cannot access PackageTest bad class file: .\PackageTest.java

In this topic and this one provide an excellent explanation about how to compile Java source code files in different folders using a similar example. I'm pretty sure after carefully reading this topic all your doubts (concerning this topic) will be cleared. If you still have doubts/questions after reading these topics, simply click on the "Post reply" button and let us know

Hope it helps!
Kind regards,
Roel
 
karunakar theru
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very useful explanation. Thank you very much Roel. 
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!