• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Enthuware Software Support
Posts: 4818
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very useful explanation. Thank you very much Roel.  
 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic