Two Laptop Bag
The moose likes Mock Exam Errata and the fly likes Initialization a variable before declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Mock Exam Errata
Reply locked New topic
Author

Initialization a variable before declaration

Sanjit Kumar
Ranch Hand

Joined: Dec 04, 2006
Posts: 35
hi everyone,

please help me to clear the following doubt.

1>what is the rule to use a variable(read or write) before declaration.

2> code snippet:
class MyClass{

static{
int x = 2 * var1; //line1
int y = 3 * MyClass.var1; //line2
}

static{
int var1 = 10;
}

In the above code line1 gives error but line2 runs successfully.I am not able to understand the behaviour. Please explain how this code is working.

How the compiler(or system) handles or process var1 in line2 which is running nicely without declaring var1 before using it at line2.

I hope i am very much clear what i am trying to ask....if not please reply back..
Thanks in advance
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 14598

First, please read this. This explains why it is important to not crosspost, and to choose the correct forum.

This forum is for the discussion of Errata. In order to do that, we need to know which mock this question is from, and why do you think it is wrong. In any case, the other copy of this topic is probably in the correct forum, so I'm locking this one.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
 
subject: Initialization a variable before declaration
 
Threads others viewed
Arrays doubt
arrays
different main method arguments
Thread question
write operation before declaration...
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture