Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Are static variables not part of a class?

 
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10835
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79531
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, that's not it. Think some more. It has to do with ownership and access.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me explain with a "stupid" program

 
keep an eye out for scorpions and black widows. But the tiny ads are safe.
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic