File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Mock Exam Errata and the fly likes Initialization a variable before declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Reply locked New topic

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{

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

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

Joined: Sep 28, 2004
Posts: 20525

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.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: Initialization a variable before declaration
jQuery in Action, 3rd edition