• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declare and Read rule

 
vinayak manda
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
vinayak manda
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
)
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
siva prakash
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic