Why does this Java casting throw an error?

I was wondering why referencing "w" after obj = w; will throw an error. Aren't you just creating another pointer to that w instance by saying obj = w? I.e. why is it different to saying something like String s = "hi"; String w = s; Thank you!

public class Casting {
   public static void main(String[] args) {
      // casting doesn't change the object
      Object obj;
      { 
          Stopwatch w = new Stopwatch();
          obj = w;
      }
      System.out.println(obj); // this line does work
      System.out.println(w); //this line does not work 
   }
}

Answers


There's nothing like quoting the JLS first thing in the morning.

JLS 6.3. Scope of a Declaration:

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

JLS 14.2. Blocks:

A block is a sequence of statements, local class declarations, and local variable declaration statements within braces.

What does it mean in your case? The local varialbe w is declared in the block

{ 
    Stopwatch w = new Stopwatch();
    obj = w;
}

("its own initializer" is the first line in the block) and so its scope is the rest of that that block. The reference to it,

System.out.println(w); 

is outside of the block and so w will not be able to resolve to a variable.

What about the local variable obj then? It was declared in the block

public static void main(String[] args) {

    Object obj;
    { 
        Stopwatch w = new Stopwatch();
        obj = w;
    }
    System.out.println(obj);
    System.out.println(w);
}

which in this case is a method block. The call

System.out.println(obj);

is inside the block, so obj can be referenced successfully.


This is the problem with the scope.The scope of

{ 
      Stopwatch w = new Stopwatch();
      obj = w;
  }

here the w scope is within brackets obly and this variable is not accessible outside.So here there is no problem of assignment but the problem is related to scope and can be solved easily by

public class Casting {    public static void main(String[] args) {
  // casting doesn't change the object
   String w;
  Object obj;
  { 
      w = new String();
      obj = w;
  }
  System.out.println(obj); // this line does work
  System.out.println(w); //this line now working     } }

Variable w is declared inside the block, thus out of the block it does not exist. This problem has nothing to do with casting. Remove braces and it will work:

public class Casting {
   public static void main(String[] args) {
      // casting doesn't change the object
      Object obj;
      Stopwatch w = new Stopwatch();
      obj = w;
      System.out.println(obj);
      System.out.println(w);
   }
}

As it metioned in other answers, removing the braces will work. Also you do as below if you want to keep the brackes. Actually in java , variables has scope, one variable can be used in scope where it is declared. Like if you declare some variable in "if" block or "try" block, it can be used in that scope only. This helps in effective garbage collection. And always it best to define variable in minimum needed scope However, if you are creating some object somewhere but you want to use it after that section (block), then declare (variable not necessary creating object) it outside the block.

public class Casting {
public static void main(String[] args) {
  // casting doesn't change the object
   Object obj;
  Stopwatch w ;
  { 
      w = new Stopwatch();
      obj = w;
  }
  System.out.println(obj); // this line does work
  System.out.println(w); //this line does not work 
  }
}

Need Your Help

iphone dev create an array accessable from a number of View Controllers

iphone arrays

I have an array arryImages which I would only like to declare once and access it from a number of view controllers, but I can't figure out where to place it.

Asp.Net/ IIS while saving record date coming in Chinese

asp.net vb.net iis-7

In my Asp.Net application when I save or update a record the date is coming as @CreatedAt = N'06-May-2015 02:49:03 下午 only the AM/PM part is coming in chinese while in systems Region and language s...