• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Why non static variable cannot be accessed from static variable?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why non-static variable cannot be accessed from static variable?. Is it because, inside Static methods we cant get the exact instance of the variable?
 
Ranch Hand
Posts: 157
1
Android MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guna, this is scope. Static members are class variables whereas non static members are instance variables. so how a class can know which object to see ?? Maybe i am not expressing it right way, try to make sense, its easy.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See Understanding Instance and Class Members in Oracle's Java Tutorials to understand what static means and what the difference is between static and non-static member variables and methods.
 
Marshal
Posts: 76403
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are 100% correct, but not using the usual words. You mean that static methods are part of the class not the object, so the JVM cannot work out which instance to look for the non‑static members in.
As for the thread title:- You cannot access non‑static members from a static method or initialiser. You cannot access anything from a variable. You can access every member of the class from an instance method or initialiser.
 
Ranch Hand
Posts: 87
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static members are initialized before object of the class is created.
Each object of the class has separate copy of Instance variables but static variables are not depend on object instead they are class variables.

e.g.



After compiling:
1 error will occur
non-static variable cannot be referenced from the static context

Because main() is static & it is loaded before any object of the class is created.
If you want to access instance variable a inside main() you have to create object of Example class.


 
Campbell Ritchie
Marshal
Posts: 76403
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually those fields might not be initialised. If you use the second code example, it will print the default uninitialised value of a, namely 0.
 
Aniket S. Kulkarni
Ranch Hand
Posts: 87
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes! You are right Campbell Ritchie.
All the instance variables are initialized to their default value.
Because of that it will print Value of a=0
 
I RELEASE YOU! (for now .... ) Feel free to peruse this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic