This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Strange behavior of compilers

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This code compiles and runs without any problem with JDK1.3. But JDK1.4 gives compilation error. Is there anything written in JLS about the "field access expression"?
What should I do if I get an expression like

in the exam?
Thanks and regards
-Manish
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Current exam doesn't include JDK1.4
And if you'll get such questions, answer them according to JDK1.2,1.3 API.
Jamal.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this compiles under 1.3, I'd say it's a bug in the compiler.
You should not be able to access a private member of the Inner class from your main method.
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the statement is
System.out.println(inst.Inner.i); then there will be a compile time error in every version. I thing the code is :
System.out.println(inst.inner.i);
"inner" ....not "Inner"
 
Manish Kumar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NO Nazmul, it is works fine with jdk1.3 and prints the value of 'i'.
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manish,
I'm using jdk 1.4 (java 1.4.0-b92), and your example compiles and runs fine.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nazmul, because the int is static, it should be fine to reference with either "inner" or "Inner". The main question has to do with the private.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javac in JDK 1.4 complains about
"
System.out.println(inst.Inner.i);
"

the result is


Q.java:9: unexpected type
required: class, package
found : variable
System.out.println(inst.Inner.i);
^
Q.java:18: i has private access in Q.Inner
{ System.out.println(new Q().getInner().i);
^
2 errors


This is from JLS 6.6.1


* Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (�7.6) that encloses the declaration of the member.


[ May 02, 2002: Message edited by: Jose Botella ]
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jose,
after reading and re-reading that quote from the JLS, it seems to me that the the private inner class member should be accessible from the outer class. What do you think?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i is accessible from Q, but not from Test.
 
Nazmul Huda Sarkar
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops!!! May be I was drunked. Anyway...Really strange behavior. It compiles on 1.3 but not in 1.4...

Originally posted by Rob Ross:
Jose,
it seems to me that the the private inner class member should be accessible from the outer class. What do you think?


You are right...even non-static private member in inner class can be accessed from outer class throgh inner class object referance.
 
Acetylsalicylic acid is aspirin. This could be handy too:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!