Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Declaration of Static variables - code standard Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Declaration of Static variables - code standard" Watch "Declaration of Static variables - code standard" New topic
Author

Declaration of Static variables - code standard

Niall Loughnane
Ranch Hand

Joined: Dec 07, 2006
Posts: 208
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

Joined: Jan 17, 2006
Posts: 1296
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.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Niall Loughnane
Ranch Hand

Joined: Dec 07, 2006
Posts: 208
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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!


[Jess in Action][AskingGoodQuestions]
Niall Loughnane
Ranch Hand

Joined: Dec 07, 2006
Posts: 208
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

Joined: Oct 13, 2005
Posts: 39053
    
  23
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
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.
 
GeeCON Prague 2014
 
subject: Declaration of Static variables - code standard