Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why non static variable cannot be accessed from static variable?  RSS feed

 
Guna sekar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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?
 
harshvardhan ojha
Ranch Hand
Posts: 157
1
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • 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.
 
Aniket S. Kulkarni
Ranch Hand
Posts: 87
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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: 55681
161
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!