Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declaration of Static variables - code standard

 
Niall Loughnane
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • 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
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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 :-)
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic