• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Forward references with array elements

 
Greenhorn
Posts: 12
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have seen that if I run something like:
--

--
I do not get any compilation error.

But if I try:
--

--
I get an "illegal forward reference" compilation error in "a[0] = 10;".

By looking into the "Java Language Specification Java SE 7 Edition" I see this snippet:
--
The declaration of a member needs to appear textually before it is used only if the member is an instance (respectively static) field of a class or interface C and all of the following conditions hold:
1. The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C.
2. The usage is not on the left hand side of an assignment.
3. The usage is via a simple name.
4. C is the innermost class or interface enclosing the usage.

It is a compile-time error if any of the four requirements above are not met.
--

For the line "a = new int [1];", I understand that we are in point number 2. So this is fine. But why line "a[0] = 10;" does it cause a compilation error?

Thanks.
 
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Jesus Saguzo

Welcome to the ranch. In the first code snippet, the field static int a[] is used in the static initializer, however, it is used during an initialization i.e. usage is on the left hand side of an assignment. In the second code snippet, the field static int a[] is initialized and then used in the static initializer. The JLS insists that if a field is used in an initializer, the declaration of the field must appear textually before it is used.

In the second code snippet, this code a[0] = 10 does not use the field static int a[] on the left hand side of that assignment. What is used on the left hand side of that assignment is an item (at index 0) in the array referenced by the field static int a[]

This issue was discussed here.
 
Gesu Saguzo
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the explanation Joe.

Topic is a bit tricky but I can understand it.

 
I think I'll just lie down here for a second. And ponder this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic