Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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

 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79642
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79642
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic