• 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

static Access And Casting Null with Access

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Puzzlers Question Page no. 166



see the line 13.
Please Tell the all process. step by  step
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a very weird piece of code. The way it is working is that when you type cast null to X.Y, compiler knows that you are referring to the inner class X.Y not the variable Y inside the class X. Then when you access the Z variable, compiler thinks you are trying to access the Z field of the inner class X.Y on an instance of that class. Since the field is static, the compiler will then replace the instance (which is null in this case) on which you are accessing Z with the class name itself, as static fields are accessed on the class not an instance of the class.
 
Marshal
Posts: 67443
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I remember correctly, this is a case of obscuring. There are more details in Java Puzzlers by Bloch and Gafter, but I might not be able to find my copy until Wednesday. That is why you should stick to naming conventions, that fields don't start with CapitalLetters. But the field Y takes precedence over the nested type Y unless you force the type to be used with the cast.
As you say, it is a weird piece of code, but I have seen questions about something like that before.
 
Ankit Garg
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:As you say, it is a weird piece of code, but I have seen questions about something like that before.


On the certification exam?

I tried this code, and depending on the context compiler can tell whether you are referring to the field or the inner class, but as Campbell said the field takes precedence when there is ambiguity

X.Y.class // inner class
X.Y.getClass() // field
X.Y abc = null // inner class
X.Y = null // field
X.Y.Z = null // field i.e. the Z variable of C class is updated
 
Campbell Ritchie
Marshal
Posts: 67443
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ankit Garg wrote:. . . On the certification exam? . . .

No. Maybe in Java Puzzlers, but I can't check that until tomorrow.
 
Ng Sharma
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ankit Garg wrote:

Campbell Ritchie wrote:As you say, it is a weird piece of code, but I have seen questions about something like that before.


On the certification exam?

I tried this code, and depending on the context compiler can tell whether you are referring to the field or the inner class, but as Campbell said the field takes precedence when there is ambiguity

X.Y.class // inner class
X.Y.getClass() // field
X.Y abc = null // inner class
X.Y = null // field
X.Y.Z = null // field i.e. the Z variable of C class is updated



Thanks Ankit, nice explanation.
 
Ankit Garg
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ng Sharma wrote:Thanks Ankit, nice explanation.


I was just building up on Campbell's insights, so thanks to him for the clarification that the field takes precedence
 
Campbell Ritchie
Marshal
Posts: 67443
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get yourself a copy of Java Puzzlers, Excellent book. Or borrow Liutauras' copy. You can't have mine, which I have now found.
Let's see what it says. Obscuring appears on pages 163‑167 and page 182. I read page 163 and knew why I have seen that sort of code before: OP: please always tell us where code comes from. Maybe somebody showed you that code and didn't tell you where it came from. You will find the version with casting nulls on page 166.

Bloch and Gafter pages 163‑164 wrote:. . . . Does the program print Black? Does it print White? Is it even legal?. . . The compiler generally rejects ambiguous programs . . . . it should be illegal . . . it is legal and prints White. . . . When a variable and a type have the same name . . . the variable name takes precedence. . . . obscure the type name.
. . . To remove all ambiguity . . . simply rewrite it to obey the naming conventions. . . .
Their emphasis.

There is a lot more; you will have to read the original to get that section in its full glory.
The book quotes two Java® Language Specification (=JLS) sections:- JLS‑§6.5.2 and JLS‑§6.3.2 (nowadays §6.4.2). They also say that class names like UUID or URL or URI would be better spelt in mixed case than upper case.
 
Ankit Garg
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yay you found it

Why can't anyone have your copy? You can have it back whenever you need it
 
You learn how to close your eyes and tell yourself "this just isn't really happening to me." Tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!