File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strange behavior  of compilers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Strange behavior  of compilers" Watch "Strange behavior  of compilers" New topic
Author

Strange behavior of compilers

Manish Kumar
Ranch Hand

Joined: Apr 15, 2002
Posts: 53

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

Joined: Jan 08, 2002
Posts: 411
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

Joined: Jan 07, 2002
Posts: 2205
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.


Rob
SCJP 1.4
Nazmul Huda Sarkar
Ranch Hand

Joined: Feb 01, 2002
Posts: 317
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"


Nazmul<br />SCJP,SCWCD,IBM OOAD with UML
Manish Kumar
Ranch Hand

Joined: Apr 15, 2002
Posts: 53
NO Nazmul, it is works fine with jdk1.3 and prints the value of 'i'.
Mag Hoehme
Ranch Hand

Joined: Apr 07, 2002
Posts: 194
Hi Manish,
I'm using jdk 1.4 (java 1.4.0-b92), and your example compiles and runs fine.


Mag
Joshua Kueck
Ranch Hand

Joined: Mar 14, 2002
Posts: 71
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

Joined: Jul 03, 2001
Posts: 2120
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 ]

SCJP2. Please Indent your code using UBB Code
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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

Joined: Jul 03, 2001
Posts: 2120
i is accessible from Q, but not from Test.
Nazmul Huda Sarkar
Ranch Hand

Joined: Feb 01, 2002
Posts: 317
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
 
subject: Strange behavior of compilers