• 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Are static variables not part of a class?  RSS feed

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

 
Saloon Keeper
Posts: 5137
54
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: 5443
147
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: 61696
193
  • 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: 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: 421
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: 12739
210
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: 1970
57
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 is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!