• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is it not syntax error to access member variable in a method not initialized  RSS feed

 
Tony Smith
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Today it took me 30 min trying to figure out an issue and it turned out that the instance is not initialized. But the IDE didn't prompt any error. So I had similar code to something below:



Thanks!
 
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
In future, while posting code, please UseCodeTags. I have added them for you this time. As you can see the code tags, make the code much more easier to read and understand.

In the code you posted, are you sure what you wrote on line 7 is what the IDE is telling you? I would suggest you take a look one more time. Hint: What does static mean?
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are breaking a rule laid down by the Java compiler that makes it imperative for you to ensure that static members (methods / variables) should only be accessed in a static context. Read the error message carefully and try and understand the significance of static keyword in Java
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:You are breaking a rule laid down by the Java compiler that makes it imperative for you to ensure that static members (methods / variables) should only be accessed in a static context.


There is no such rule in the Java language or compiler.

However, it is good practice to minimize access to static members in a non-static context, and to not reference them through a reference variable.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Smith wrote:


No, it will tell you that you can't access a non-static variable in a static context. It won't say anything about it not being initialized.





Member variables get default values at object instantiation, so it's not a compile time error to read those variables before explicitly initializing them, though it will often lead to undesirable results at runtime, such as NullPointerException.

Local variables, on the other hand, do not get any default values, so we have to assign a value to a local before we can read it.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote: . . . static members (methods / variables) should only be accessed in a static context. . . .
I think you have mistakenly written static when you meant non‑static.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Mansukhdeep Thind wrote: . . . static members (methods / variables) should only be accessed in a static context. . . .
I think you have mistakenly written static when you meant non‑static.


Yes Campbell, I wanted to say that non static members cannot be accessed from a static context.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Mansukhdeep Thind wrote:You are breaking a rule laid down by the Java compiler that makes it imperative for you to ensure that static members (methods / variables) should only be accessed in a static context.


There is no such rule in the Java language or compiler.

However, it is good practice to minimize access to static members in a non-static context, and to not reference them through a reference variable.

Sorry Jeff. I meant to say that non static members cannot be accessed from a static context. I mistakenly wrote static where I should have written non static.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don’t worry. Everybody makes such mistakes every now and again.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Don’t worry. Everybody makes such mistakes every now and again.


The wonderful thing about this site is that when you do make a mistake, there are a dozen people ready to point it out to you (and to the rest of the world).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!