File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Declare and Read  rule Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Declare and Read  rule " Watch "Declare and Read  rule " New topic
Author

Declare and Read rule

vinayak manda
Ranch Hand

Joined: Jul 27, 2004
Posts: 31
Hi all,

Please have a look at the below code :

Class A {
int a = 7*b; // legal forward reference
static int b=4; }

Since a class is always initialized before it can be instantiated,an instance initializer expression can always refer to any static member of a class. // I agree with this statement

But what happens here in this code below :
class B {
int a=b=20; //legal forward reference
int b;
}

I found this code in Khalid Mughal'c book (Page No 333) and it says that the statement is legal,here I'am confused because I thought it will be legal only when b is static
Can you explain how it works....
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
That statement is legal because you're not really grabbing the value from b, at all. When you perform an assignment, you place the value to be assigned into the variable and then that value is returned. The fact that the value is returned is normally ignored.

So, in this case, you assign a value to b, that value is returned, and you then assign that same value to a. That is perfectly legal and does not result in a forward reference error.


SCJP Tipline, etc.
vinayak manda
Ranch Hand

Joined: Jul 27, 2004
Posts: 31
Thanks Corey ,

I got the assignment part ,,but I'am confused whether compiler makes a check if b is declared or not before the assigment is done.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
In:


the a = b = 20; means assign to a the result of (b = 20) which is 20.
b = 20 is a legal forward reference to b because the b appears on the left-hand side of the assignment.

Mughal explains all in the last paragraph at the bottom of page 332 of his 2nd Edition SCJP 1.4 book. (No, I'm not going to type it all in here
)


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by vinayak manda:
Thanks Corey ,

I got the assignment part ,,but I'am confused whether compiler makes a check if b is declared or not before the assigment is done.


But keep in mind what we're talking about when we're discussing "forward references."

A forward reference isn't a reference to something that is not yet declared (at least not in this case) - it is a reference to a variable that has not yet been initialized.

When we first begin object initialization, we must allocate some memory of the heap for outr object. With that, we must allocate for any member variables our object has, including a and b. That means that, before any initialization code executes, we already have variables a and b sitting on the heap. They already exist, they just haven't been initialized.

So the compiler isn't checking to see if b is declared or not, it's checking to see it b is initialized or not. In this case, however, no check is necessary because we're not assigning the value of b to a, we're assigning the return value of the assignment to a.

I hope that helps,
Corey
siva prakash
Ranch Hand

Joined: Aug 24, 2004
Posts: 80
Hi Corey,

iam getting compile time error with jdk1.4.0 version , with following code :

int a = b = 10 ;
int b ;


but ur saying , it won't.


pls explain


With Regards ,

Siva
Nandish Sri
Ranch Hand

Joined: Jan 19, 2004
Posts: 45
hi Siva Prakash,
I think u r getting compiler error because u r using those assignment in a method , IN A METHOD .It should be in class level. Then u wont get any errors.

Regards,
Nandish


Nandish
siva prakash
Ranch Hand

Joined: Aug 24, 2004
Posts: 80
Hi nandish ,

iam getting compile time error even i placed the code into class level.


Pls help me in this regard,


bye
siva
Nandish Sri
Ranch Hand

Joined: Jan 19, 2004
Posts: 45
hi siva prakash,
see here is the code with which I tested,

package Test;
public class Test {
int a=b=3;
int b;
public static void main(String argv[]) {
/* Error here

int c=d=3;
int d;

*/
}
}

Try once again..& let me know !
Regards,
Nandish
Prasath Thirumoorthy
Ranch Hand

Joined: Jul 23, 2004
Posts: 65
Hi Mr Sree nandeesh,

package test;
public class B {
int a = b = 20;
int b;
public B(){
}
}

this is the code i written in Netbeans 3.6 IDE
im using jdk 1.4 version only
but it saying Illegal forward reference

plz help me

Thanks & Regards
prasath
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Declare and Read rule