• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

inner class using forward reference doubt

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




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 ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 363
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't give a value to the variable before you declare what it is.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you think about this code?
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mhhh...
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Keith and Anton.I got it after lots of execution in a different ways.
And thanks for consistently helped for my understanding.
 
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic