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 Programmer Certification (SCJP/OCPJP) and the fly likes language fundementals 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 » Programmer Certification (SCJP/OCPJP)
Bookmark "language fundementals" Watch "language fundementals" New topic
Author

language fundementals

Anuji Philip
Ranch Hand

Joined: Feb 25, 2002
Posts: 46
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?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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.


Dave
Sharon Miller
Greenhorn

Joined: Mar 25, 2002
Posts: 10
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

Joined: Dec 20, 2001
Posts: 3271
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
Mag Hoehme
Ranch Hand

Joined: Apr 07, 2002
Posts: 194
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 ]

Mag
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: language fundementals
 
Similar Threads
Forwrad Referncing
Restrictions on the use of Fields during Initialization
Question on Local Classes in java
forward referencing doubt
Array