wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inner class using forward reference doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inner class using forward reference doubt" Watch "inner class using forward reference doubt" New topic
Author

inner class using forward reference doubt

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477




Please help me with my doubts i got?

[ February 13, 2007: Message edited by: Shiva Mohan ]

[ fixed overlong line which was screwing up the format for the rest of the page - Jim ]
[ February 13, 2007: Message edited by: Jim Yingst ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You are mixing instance and class variables.

The class variables are going to be processed the first time the class is loaded into memory.

The instance variables are initialized each time an instance of the class is created.

The class variables are processed before the instance variables.

So its fine for an instance variable to use the value of a class variable declared later.

But in your second example, you are trying to use a variable without declaring it's type. Notice that since you don't use the keyword static when you declare it, i1 is an instance variable, but you don't declare it's type.
[ February 13, 2007: Message edited by: Keith Lynn ]
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Hey Shiva!
Don't you see that you are using it before declaring it?
Remember,any variable you use must be declared before you use it. However there is an exception to this; the local variables must be initialized too! That means, any local variable you use must be declared as well as initialized before you use it.
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Yes Keith.I got all your explanation.Those are very clear.Thanks.

But


When i declare the line 1 variable(i1) instance type,still the earlier one gives error as identifier expected.Why?
[ February 13, 2007: Message edited by: Jim Yingst ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You can't give a value to the variable before you declare what it is.
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Please refer the yesterday post of mine.on that post, you helped me with the question which is very similar to the previous one that states that
your code does not violate forward referencing rules, because you are not using the value of a variable that is declared later.

http://www.javaranch.com

Please help me undesratnding how is is showing error inside inner class after referring my link post.Thanks.
[ February 13, 2007: Message edited by: Shiva Mohan ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
The code yesterday was something like this



The scope of the variable x in this case is the entire class definition including the static initializer block.

The important thing to remember when it comes to forward referencing is that it is not a concern unless you are trying to use the values of variables that are declared textually after the point where you try to access the value.

In this case, you are not trying to use the value of x, you are simply assigning a value to x.

In this code



this doesn't have anything to do with forward referencing. The variable x is not known until you declare it.
Anton Uwe
Ranch Hand

Joined: Jan 10, 2007
Posts: 122
What do you think about this code?
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
In this case, you are not trying to use the value of x, you are simply assigning a value to x.


In this code

code:
x = 10;

static int x = 15;

this doesn't have anything to do with forward referencing. The variable x is not known until you declare it.(since x is static(second line) ,and in the first line we declared x is a instance variable.so that instance variable x type is not declared yet error is coming.)i got it.


like that way,i gave

i1=1;
int i1=5;//note not a static variable.


inside the inner class.But this is giving compile error.

I thought i am not trying to use the value of i1,i am just simply assigning a value to i1 on the first line.so this is not violation forward reference rule.so this won't give compile error.But this is giving compile error.

program for verification.



Am i making any sense with my question?

[ February 13, 2007: Message edited by: Shiva Mohan ]
[ February 13, 2007: Message edited by: Shiva Mohan ]
Anton Uwe
Ranch Hand

Joined: Jan 10, 2007
Posts: 122
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I think you're still not clear on what forward referencing means.

Forward referencing occurs when are you trying to use the value of a variable before that variable has been declared.

In this code,



, we are not violating forward referencing rules.

The only way it would violate the forward referencing rule is if, in the static initializer, we tried to use the value of x.

For example, this is an illegal forward reference.



This is not legal because the declaration of x occurs textually after its use, and x is on the right-hand side of the assignment statement.

That means we are trying to use the value of x before we know what it is.

In the case of



this has nothing at all to do with forward referencing.

This will not get past the compiler because you have to declare the variable before you use it in this code.
Anton Uwe
Ranch Hand

Joined: Jan 10, 2007
Posts: 122
mhhh...
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Thanks Keith and Anton.I got it after lots of execution in a different ways.
And thanks for consistently helped for my understanding.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: inner class using forward reference doubt