• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

language fundementals

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)class Q1
{
int i = j;
int j = 2;
}//compiler error as forward referencing

class Q2
{
int i = method();
void method(){System.out.println(j);}
int j = 2;
}//compiles properly.
As far as I know,instance variables get default values before instance initilization and object creation.So I could explain clearly why Q2 compiles.But in same lines I couldn't explain Q1.I heard of forward referencing and JAVAC is a single pass compiler.But want to know in detail.
2)Why in for and while loop I get unreachable statement error,but not in if or switch ,for same conditions same error?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried executing the code in Q2? The output that you see might give you the answer that you need.
In short, when members are initialized, they are done from top to bottom. If you try to reference a member that it "below" your current point of execution, the compiler will complain.
Try executing the code in Q2 and see what you get as output. I think you might be surprised and it may help you clear up this issue.
Corey
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ANUJI
For your second question you can check out the JLS section 14.20. Basicaly, they allow the if-then to work so that you can use conditional debugging in your programs.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just for completeness sake, I wanted to add the following to the discussion.
The only time a "forward declaration" will successfully compile is when j is declared as a static member variable.

Static member variables are loaded and initialized when the class is loaded. Instance member variables are loaded and initialized when an object of the class is instantiated.
-Sharmila
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
However, you can have forward reference problems just like this one when using static members, like this:

This will lead to the exact same problem as when both members were instance members. The reason that making j static fixes the problem is due to the fact that j would be initialized prior to i.
Corey
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd like to add my interpretation of the code - which did not compile as it was. Therefore I've modified the code as follows:

At a first glance, the output is indeed somewhat surprising:
j = 0
i = 0
I guess the reason is that at first all (member) variables declared in a class are initialized to zero.
However, I'm a bit lost in trying to understand why the declaration (int j = ...) and the assignment of a value (int j = 2) are separated by the method call (without any compiler complaints).
Any explanation is welcomed. Thank you.
[ April 23, 2002: Message edited by: Mag Hoehme ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, Mag, you saw what I wanted you to see. The fact that j is still 0 when you call the method "method()" is important. You see, initialization hasn't yet reached the line that says "int j = 2;". Therefore, when you invoke method(), j has a value of 0 (it's initial default value).
Remember, member initialization is done in order from top down. Therefore, i gets initialized (which calls method()) and then, after method() has been called, j is initialized.
The reason this compiles is because the compiler doesn't look inside method() to see if there are any forward references. It'll only look to see if you're trying to directly assign a value that hasn't been initialized yet to a variable. That's why your initial code failed to compile.
Order of initialization can be tricky, but it's also quite important and it's most definitely open game on the exam. Check out the JLS, §12.4 Initialization of Classes and Interfaces for all sorts of nit-picky details.
I hope that helps,
Corey
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic