This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Java in General and the fly likes Odd Initialisation requirement? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Odd Initialisation requirement?" Watch "Odd Initialisation requirement?" New topic

Odd Initialisation requirement?

Marc West

Joined: Dec 19, 2011
Posts: 5

I didn't know whether to put this in the beginner section or not. Hopefully it's the right place.

I'm curious why does below code require variable to be initialised despite code logic only ever accessing it if it has been initialised? Something to do with how Java compiles?

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 44041
Welcome to the Ranch

It looks like something out of a convoluted C competition! Where did you find it?
Marc West

Joined: Dec 19, 2011
Posts: 5

I simply ran into this oddity while writing code for a coursework. The above is just a barebones example. I am sure I have seen this issue before, possibly when doing C++. Curiosity got the better of me.
Tim Moores

Joined: Sep 21, 2011
Posts: 2409
While it's obvious to a human observer that "object" is never accessed, it's not to a compiler. To be on the safe side, the compiler needs to evaluate and compile the entire source code. Eliminating unused code sections is an optimization that takes extra work on the part of the compiler writer, and (at runtime) on the part of the compiler.

What's more, while the standard Java compiler (javac of the Oracle JDK) does detect, and optimize away, some similar code, this is actually tricky business. For example, the code you wrote (or rather the class it's in) uses the MyClass class - which means the class would need to be loaded when the class containing this code is executed (assuming that it hasn't been loaded before) - that would involve loading the MyClass class and executing its static initializers, which could do all kinds of things with side effects. If the optimizing compiler were to eliminate all traces of MyClass from this code, the resulting code might no longer work in the same way as the unoptimized code does. A bit of a contrived example, but it does point to some of the problems optimizing a language like Java raises.
Marc West

Joined: Dec 19, 2011
Posts: 5
I am not sure I fully understand what you are trying to communicate.

Tim Moores wrote:While it's obvious to a human observer that "object" is never accessed, it's not to a compiler.

Tim Moores wrote:If the optimizing compiler were to eliminate all traces of MyClass from this code [...]

Looking at the above, perhaps you've misread the code and gave your answer from there? The object does get accessed, but only if it first becomes initialised. So it's not a matter of taking out the second if statement through optimisation process, it's a matter of seeing that the second conditional branch only executes when the first one does.

P.S. In this contrived example, the compiler could even see that on top of that, both if statements always execute, so the statements may as well be extracted into the method block which contains them (I know it's not illustrated but I don't think you can put if statements straight into a class block). But why it doesn't do this , is a second, seperate question unintentionally created by my choice of example.

In my naivity I wrote what I thought was a simplified but accurate example of my actual code without quickly running it. I was wrong. My example code executes without problem. Apparantly the reason it was complaining about potential unitialisation was because within my if statement I was retrieving an object through another class's getter function, but that function itself didn't guarantee I would be returned an initialised object.

I am dully impressed by how thoroughly the IDE checks potential for uninitialised object.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Odd Initialisation requirement?
It's not a secret anymore!