• Post Reply Bookmark Topic Watch Topic
  • New Topic

The error occurs unless initializing the variable. Why?  RSS feed

 
Hasan Fatih
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand that the error occurs when I don't initialize the variable myPoint. Whereas, I initialize the variable myPoint after variable declaration and before place of error. What's the problem?



When I initialize the variable myPoint to zero in its decleration, the error disappear. But why? I have already initialized the variable myPoint in default case before line of error occured.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not always initialized. The default case only runs when the other cases don't match. Since the possibility exists for the variable to be un-initialized, Java complains.
 
Hasan Fatih
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:It's not always initialized. The default case only runs when the other cases don't match. Since the possibility exists for the variable to be un-initialized, Java complains.


Yes, it's not always initialized, but at the same time the program isn't always going into while statement. Just when the default case runs and initializes variable myPoint, program runs while statement, and uses variable myPoint in the while statement. So ... ?
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your switch statement, the variable may or may not be initialized. Then later there's this line of code:



Here, as far as the compiler is concerned, the variable may not have been initialized. If you're saying that you expect the compiler to analyze your code and conclude that this line of code is only run in case an earlier line of code which initialized the variable was run, well, no, the compiler doesn't do that.
 
Hasan Fatih
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:In your switch statement, the variable may or may not be initialized. Then later there's this line of code:



Here, as far as the compiler is concerned, the variable may not have been initialized. If you're saying that you expect the compiler to analyze your code and conclude that this line of code is only run in case an earlier line of code which initialized the variable was run, well, no, the compiler doesn't do that.


So, Can we say that compilers is not smart enough?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can say the compiler isn't looking for matching conditionals. As for whether it is smart enough, that is a human judgement call.
 
Hasan Fatih
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:We can say the compiler isn't looking for matching conditionals. As for whether it is smart enough, that is a human judgement call.


Okay, thanks guys for your replies.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hasan Fatih wrote:So, Can we say that compilers is not smart enough?


No. For example in that code there might be the possibility that it's being executed by two threads at the same time, and a race condition might occur, leading to use of an uninitialized variable. There's no reason to write a compiler which attempts such complex logic, that just makes it slower and less reliable.
 
Hasan Fatih
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm... Okay. Thanks for your explanation.
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote: . . . As for whether it is smart enough, that is a human judgement call.
You design a compiler as far as you design the compiler. As Paul C said, it would make it too complicated to create a compiler which covers all possibilities like that.
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:We can say the compiler isn't looking for matching conditionals. . . .
I looked through the index for the Java Language Specification and didn't find anything relevant.
 
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
To add (more confusion )
Now edit your code to declare it as an instance variable instead.

Now what do you observe?
 
Hasan Fatih
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:To add (more confusion )
Now edit your code to declare it as an instance variable instead.

Now what do you observe?


Oppss.. The problem has gone with that way. No problem anymore. But why? I am confused.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because non-local variables (static fields or instance fields) actually do get instantiated; if no explicit value is given they will be given a default value (false for booleans, 0 for numbers, '\0' for chars, null for objects).
 
Hasan Fatih
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Because non-local variables (static fields or instance fields) actually do get instantiated; if no explicit value is given they will be given a default value (false for booleans, 0 for numbers, '\0' for chars, null for objects).


I was supposing that once a new object was created(instantiated) from a class, the fields of object initialized to default value automatically. Although I don't create an object of the class, the fields are initialized to default value as you said. That's interesting. Thank you for your advice and explanation.
 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a rule of thumb, should we always initialize variables, whether they are local or otherwise?
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Remember local variables must be initialised.
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It used to say in the Java Tutorials that it is poor style to rely on fields having default values, and to reassign those fields (even if to 0) in the constructor. But I can't find that in the recent versions.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!