File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange behavior of compilers

 
Manish Kumar
Ranch Hand
Posts: 53
  • 0
  • 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
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • 0
  • 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.
 
Rob Ross
Bartender
Posts: 2205
  • 0
  • 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.
 
Nazmul Huda Sarkar
Ranch Hand
Posts: 317
  • 0
  • 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
  • 0
  • 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'.
 
Mag Hoehme
Ranch Hand
Posts: 194
  • 0
  • 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.
 
Joshua Kueck
Ranch Hand
Posts: 71
  • 0
  • 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.
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic