0

I'm very new in java.I have a question about Forward Referencing.A forward reference is a reference to a variable that has not yet been initialized. but when we do this in method , the type of error gets different ? in fact my question is when we initialize a variable in method with another variable , is this a usage of reference or not ?

 public static void main(String[] args) {

        int total=number + 10;
        int number;

Error is :

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
  symbol:   variable number

Without a method:

class Person 
{

    int total=number + 10;
    int number;

Error is :

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - illegal forward reference
Makoto
  • 104,088
  • 27
  • 192
  • 230
Ali
  • 27
  • 2

2 Answers2

1

This is about scope (emphasis added):

  • The scope of a local variable declaration in a block (§14.4) is the rest of the block in which the declaration appears, starting with its own initializer and including any further declarators to the right in the local variable declaration statement.

and

  • The scope of a declaration of a member m declared in or inherited by a class type C (§8.1.6) is the entire body of C, including any nested type declarations.

In the first case, you're declaring a local variable, number, so you can only reference it after the declaration (but even then, you can only read it once it has been definitely assigned). If you try to reference the variable before its declaration, it is as if the variable is not there (hence "cannot find symbol").

In the second case, you're declaring a member variable, so you can reference it anywhere in the class, including before its declaration.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
-2

Forward referencing doesn't apply in the context of local variables. What applies is definite assignment. Section 16 of the JLS speaks to this.

For every access of a local variable or blank final field x, x must be definitely assigned before the access, or a compile-time error occurs.

In your case, number must be definitely assigned before it is accessed by int total = number + 10, which explains why you're getting a compile-time error.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • Agree on the illegality of the code. But it's curious to me that he's getting a runtime exception, not a compilation failure. Is this some sort of IDE effect? Maybe it replaces illegal code with something that will throw a runtime exception? –  Apr 04 '19 at 22:29
  • 1
    @another-dave: There's *no way* that this is a runtime exception. This is 100% a compilation failure unless the OP is using the `JavaCompiler` class in whichever application they're running this code in. Even then...still a compilation failure. – Makoto Apr 04 '19 at 22:30
  • The error message posted by the OP says "RuntimeException in thread 'main'". It seems bizarre to me. –  Apr 04 '19 at 22:33
  • I've tried the exact code in the first example of the Question and get a compile error: "test.java:3: error: cannot find symbol [...] symbol: variable number" – Nick Vitha Apr 04 '19 at 22:34
  • Example 2 I get a compiler error as well "test.java:2: error: illegal forward reference" – Nick Vitha Apr 04 '19 at 22:37
  • There's quite a lot of mention of "uncompilable source code" exceptions around the net, including on stackoverflow. It seems to correlate with some netbeans IDE. It seems like a negative feature to me. –  Apr 04 '19 at 22:42
  • 1
    This is wrong, it's not definite assignment, it's scope. _The scope of a local variable declaration in a block (§14.4) is the rest of the block in which the declaration appears, starting with its own initializer and including any further declarators to the right in the local variable declaration statement._ and _If an expression name consists of a single Identifier, then there must be exactly one declaration denoting either a local variable, formal parameter, or field in scope at the point at which the Identifier occurs. Otherwise, a compile-time error occurs._ – Sotirios Delimanolis Apr 04 '19 at 22:43
  • @another-dave runtime-exception is because of how the OPs IDE is working. Probably, searching on this error I found this: https://stackoverflow.com/questions/2333285/java-lang-runtimeexception-uncompilable-source-code-what-can-cause-this – marcinj Apr 04 '19 at 23:38
  • I find the argument about this being about scope over definite assignment unconvincing @SotiriosDelimanolis, so no, I won't be removing my answer. – Makoto Apr 07 '19 at 15:56
  • The formal rules about definite assignment in that chapter have a precondtion that states: _Throughout the rest of this chapter, we will, unless explicitly stated otherwise, write `V` to represent a local variable [...] **which is in scope**_ There is no _definite assignment_ analysis for a variable that doesn't exist/an identifier that can't be used. – Sotirios Delimanolis Apr 07 '19 at 16:06
  • @SotiriosDelimanolis: I got the inspiration for my answer from Example 6.3-2 in Scope. It touches on a scenario in which a variable which is in scope can be used because it is definitely assigned. It's no quantum leap to extrapolate that example to two variables and expect them to be definitely assigned before they are used. – Makoto Apr 07 '19 at 16:16
  • I don't follow. In that example, the local variable `x` is in scope when it's used in `(x = 2)` (and, sure, definitely assigned by the time it's used in the `println`). In the OP's example, `number` is not in scope. It's not the same situation. – Sotirios Delimanolis Apr 07 '19 at 16:28
  • @SotiriosDelimanolis: Since you seem to have more of an impetus on scope being a more correct answer, perhaps you should write an answer which definitely addresses the concerns from our comment chain. – Makoto Apr 07 '19 at 17:15