This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Are static variables not part of a class?  RSS feed

 
Ranch Hand
Posts: 424
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?

 
Bartender
Posts: 5632
56
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.
 
Sheriff
Posts: 5745
148
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.
 
Marshal
Posts: 63383
205
  • 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.
 
Ranch Hand
Posts: 483
23
AngularJS Eclipse IDE Firefox Browser Java Spring VI Editor
  • 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: 424
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
 
Sheriff
Posts: 13155
219
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.
 
Bartender
Posts: 2164
92
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me explain with a "stupid" program

 
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!