• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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: 80281
432
  • 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: 80281
432
  • 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 guess everyone has an angle. Fine, what do you want? Just know that you cannot have this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic