Rahul Guliani

Greenhorn
+ Follow
since Aug 19, 2012
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
5
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Rahul Guliani

Campbell Ritchie wrote:Try it and see what happens.



Compile time error : "the local variable 'event' may not have been initialized"

But, 'event' is initialized inside try-block, then, why this error? And, if the scope of local variable is within the try-block, then, we've declared event outside try-block, can't we just declare outside try-block and initialize inside try-block?

Edit:
So i tried on other blocks than try, like inside for-loop, or some other functional blocks, we have to initialize the local reference variable to NULL, then only we can use that local variable outside the block. Like in the example below :

But, i'm not getting it why we have to initialize the local variable to NULL, as inside the block the reference of that local variable is ultimately given to some other concrete object. Then, why first we are giving reference to NULL??

P.S.: Sorry for grammatical mistakes, m not a native English speaker ;)
9 years ago

Campbell Ritchie wrote:
If you need to initialise it to something, the only thing which won't produce an Exception is null.



Why we've to initialize local variable 'event' to null? Why not just declare 'event' without initialization?
9 years ago

Rahul Guliani wrote:



Hey! @Campbell can you clear my another doubt :/ it's not related to array or list. Why we declare "MidiEvent event = null" outside try bock, why not directly declare n initialize inside try-block as "MidiEvent event = new MidiEvent(message, tick)". :/
9 years ago
Thanks @Campbell for your response No there is not any difference. But i confused here why in the book they used ArrayList, whereas they could simply use Array. The code would be much simpler. yeah i know ArrayList can grow dynamically, but there is no need to it.
9 years ago
Hey! i wrote this code for making a beat-box as written in head first java, instead of using using ArrayList for check-boxes, i applied simple array.
Will there be any difference in the final app.





9 years ago