• Post Reply Bookmark Topic Watch Topic
  • New Topic

Are static variables not part of a class?  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
watching a tutorial where he said "if you create a static variable its not part of the class..." what does that mean?
and do you have to use static methods on static variables

does it just mean that the static variable is loaded in to memory for the entire program to see?
so its not part of the instantiated class?

 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:watching a tutorial where he said "if you create a static variable its not part of the class..." what does that mean?
and do you have to use static methods on static variables

does it just mean that the static variable is loaded in to memory for the entire program to see?
so its not part of the instantiated class?


Static variables are not part of a class instance, there is only one copy for the run-time of the program, not one for each instance. They are made available when the class is loaded. Scope of the variable is controllable, e.g. private or public.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
watching a tutorial where he said "if you create a static variable its not part of the class..." what does that mean?

I don't know what it means specifically.  I've always thought of static variables as "belonging to the class" and not an instance of the class.

and do you have to use static methods on static variables 

No.  Using static variables in instance methods is common.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell us which tutorial, so we know to avoid it in future. Static fields are members of a class, but not members of an object.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon wrote:"if you create a static variable its not part of the class..."
Actually the part-"part of the class" is not pretty clear here.if you will show us the-context in which it is used then we could be more specific about the answers.
Jon wrote:do you have to use static methods on static variables
static members can be accessed in a static as well as in a non static context,but if you will try to use a non static member in a static context then compiler will give you an error(Why?Think about it...)
Jon wrote:does it just mean that the static variable is loaded in to memory for the entire program to see?
so its not part of the instantiated class?
As "campbell" well mentioned static fields are members of a class and every instance of a class use the same copy of this member i.e.,unlike instance field,which is unique for the different instances of class,the static field remains a same for all the instance(the same copy of static member is visible from the different instances).so the static member gets allocated a memory only once and it is during a class is loaded.
you can go through this link https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html for understanding the static members.and one thing to note is the static blocks are not the member of a class(like constructors and instance initializer).

Kind Regards,
Praveen.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"static members can be accessed in a static as well as in a non static context,but if you will try to use a non static member in a static context then compiler will give you an error(Why?Think about it...) "
as it is potentially referencing an object that no longer exists
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that's not it. Think some more. It has to do with ownership and access.
 
salvin francis
Bartender
Posts: 1660
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me explain with a "stupid" program

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!