• 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

Declaration of Static variables - code standard

 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

in terms of Java coding standards, and i know its different for everyone,

but :-)

when i see code within a class that contains global static variables such as

public static final String name = "asdf";

and the method that the variable is used within is non-static, such as:

private void testMethod(){....}

then i don't like the variable being declared static, cause if items (methods, variables) are called in a non-static variables, they shouldn't be declared static,

like if the above variable is used in a non-static method then whats the point in having the above variable being declared static and not being booted up when the class is created,

what do you think?

Cheers

Niall
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you think it makes more sense for each instance of a class to have its own copy of a constant value. Or to put it a different way, just because some data is constant regardless of the state of an object, it doesn't mean that an object's state shouldn't access that data.
 
Niall Loughnane
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but to free up memory that the variable would hold, no matter how small, is better,

i.e. setting the variable to non-static only creates and sets the variable when the class is booted up, like there is no need to set the variable as static and just to use it within a non-static instance?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a variable is static, then there's one copy, and one copy only.

If it's not, then every single object has its own copy.

I don't understand how making many copies of something qualifies as "freeing up memory" -- unless you've got a class the average number of instances of which is near zero.

But the funny thing is that if the variable is non-static, and it's initialized in a constructor or initializer, then the code that does that initialization has a reference to a single copy of the data -- i.e., a static copy -- that is used to do that initialization. So even if the variale is non-static -- there's a static copy of the data!
 
Niall Loughnane
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your right, i was getting my ideas wrong, static variables are created once and only once and loaded into memory when the java application/project is initially booted up,

but while the instance that creates the constructor of the class holds on to a copy of the value of the non-static variable, the "static" variable data",

the "static" variable data may be nulled or removed by the garbage collector but the instance of the class still contains the data :-)
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's the other way round; when an instance is garbage collected its fields are lost, but static members are retained in memory until the class and its class loader are garbage collected.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
I think it's the other way round; when an instance is garbage collected its fields are lost, but static members are retained in memory until the class and its class loader are garbage collected.


Yes, primitives and references are. But if it's a non-final reference then it can be set to null, and the object can be garbage collected if there are no other references.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I see what you mean Rob; if the reference is set to null and there are no other references the object is eligible for garbage collection.
 
My cellmate was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic