• Post Reply Bookmark Topic Watch Topic
  • New Topic

Forward reference  RSS feed

 
Saba Shahrukh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Please help me with this code.
I don't understand how we can use variable j in test() method before the static initialiser.
I would also like to know in which situations forward reference is allowed.
 
Saba Shahrukh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And also why this code does not work and gives illegal forward reference


 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please indent your code correctly; you are not constrained by space and there is no need to squash it all together.
Have a look at the Java Language Specification and see if it helps.
 
Saba Shahrukh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone please help with the forward reference in these codes.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saba Shahrukh wrote:Can anyone please help with the forward reference in these codes.

Did you check the link Campbell provided?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Have a look at the Java Language Specification and see if it helps.

That has the info for interfaces. For classes, with a bit more detail see this part of the JLS.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That does look like a better link. Thank you.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saba Shahrukh wrote:Can anyone please help with the forward reference in these codes.



As mentioned, you should look into the JLS, if you want to have such detail... but basically, if you don't want to.

Static variables are initialized in order. And the Java Language Specification states certain ways of making sure that the program does not use the static variable before it gets initialized (such as your second example).

On the other hand, the JLS doesn't take care of all cases (nor do I think that they can), and there are some cases where you can use the variable before it is initialized (such as your first example).

Hope this helps,
Henry
 
Saba Shahrukh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to this forum, did not know how this works so was kind of left behind.

However all the above posts from Campbell, Steve did help.

That has the info for interfaces. For classes, with a bit more detail see this part of the JLS.

Have a look at the Java Language Specification and see if it helps.


Thanks.

And thank you Henry.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!