This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » 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: 18509
    
  40

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)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Initialization a variable before declaration
 
Similar Threads
Arrays doubt
arrays
Thread question
different main method arguments
write operation before declaration...