She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
Tony Morris
Java Q&A (FAQ, Trivia)
"I'm not back." - Bill Harding, Twister
"I'm not back." - Bill Harding, Twister
Originally posted by Steve L. Williams:
Anyway, is my approach(minus the final modifier) a good way to store program-wide variables.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
"I'm not back." - Bill Harding, Twister
Originally posted by Steve L. Williams:
According to everything I have read static final variables are constants. Perhaps I misread I got severely annoyed when he tried to make his point by self-referencing.
I looked at his approach, but the variables values are not known at compile time, is this still going to work?
[ May 20, 2006: Message edited by: Steve L. Williams ]
Tony Morris
Java Q&A (FAQ, Trivia)
Originally posted by Jim Yingst:
I agree with Ilja on this. But just to undermine his point, I offer one other way to fulfil the original goal of initializing a static final:
This is very much like using a static block.
Tony Morris
Java Q&A (FAQ, Trivia)
"I'm not back." - Bill Harding, Twister
I know Tony can rub some people the wrong way (trust me, I know) but that doesn't make him wrong.
Tony Morris
Java Q&A (FAQ, Trivia)
"I'm not back." - Bill Harding, Twister
Tony Morris
Java Q&A (FAQ, Trivia)
42
Originally posted by Steve L. Williams:
For example, MAXSIZE sets how many commections to the server are allowed at one time. The server keeps track of how many connections and compares it to MAXSIZE and behaves accordingly. If MAXSIZE get changed, so what? The server won't really care or behave differently.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Jeroen T Wenting:
We've got a rule here which is printed large and put on the wall: "The only good global is a global you're removing".
Live by that.
Tony Morris
Java Q&A (FAQ, Trivia)
I suppose I should note that at the moment I have no idea what particular part of HFJ you are referencing here. Care to provide a quote? K&B are more than willing to make corrections if they're necessary.
"I'm not back." - Bill Harding, Twister
Originally posted by Steve L. Williams:
In Java no, but in C, yes it can. So there is such thing as global variables. Something to be avoided but they still have their place, as does the much hated(and rightfully so)goto statement. It all depends on the situation.
page 282 at the top is large black letters: "Static final variables are constants", and goes on with an example of PI from the Math class. Guess what, it can be called all over my program if I wish it! It must be bad programming then. It also calls them that elsewhere.
By the way, what possible relevance is your opinion that Kathy is 'hot'. I hope she smacked you for being sexist.![]()
[ changed code tag to quote tag because... well... it's a quote. QED - Jim ]
[ May 25, 2006: Message edited by: Jim Yingst ]
4.12.4 final Variables
A variable can be declared final. A final variable may only be assigned to once. It is a compile time error if a final variable is assigned to unless it is definitely unassigned (�16) immediately prior to the assignment.
A blank final is a final variable whose declaration lacks an initializer.
Once a final variable has been assigned, it always contains the same value. If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object. This applies also to arrays, because arrays are objects; if a final variable holds a reference to an array, then the components of the array may be changed by operations on the array, but the variable will always refer to the same array.
Declaring a variable final can serve as useful documentation that its value will not change and can help avoid programming errors.
In the example:
class Point {
int x, y;
int useCount;
Point(int x, int y) { this.x = x; this.y = y; }
final static Point origin = new Point(0, 0);
}
the class Point declares a final class variable origin. The origin variable holds a reference to an object that is an instance of class Point whose coordinates are (0, 0). The value of the variable Point.origin can never change, so it always refers to the same Point object, the one created by its initializer. However, an operation on this Point object might change its state-for example, modifying its useCount or even, misleadingly, its x or y coordinate.
We call a variable, of primitive type or type String, that is final and initialized with a compile-time constant expression (�15.28) a constant variable. Whether a variable is a constant variable or not may have implications with respect to class initialization (�12.4.1), binary compatibility (�13.1, �13.4.9) and definite assignment (�16).
Originally posted by Steve L. Williams:
Actually I am using them as program wide variables, not just in the class. Is this the way to go about it?
I guess it doesn't have to be final since the variables are unique to this program. Are there security issues with having these variables public and not final? This is going to be a server app.
Originally posted by Tony Morris:
We probably shouldn't digress on this thread, but I'd be curious to hear your definition of "global". I contest that so such thing can exist.
42
Originally posted by Jeroen T Wenting:
In Java, not outside classloader scope. In other languages, they're all over the place.
A static member in Java is essentially global to the classloader, and as most applications run inside a single classloader they're scoped to the entire application and therefore essentially global.
Tony Morris
Java Q&A (FAQ, Trivia)
Originally posted by Ken Blair:
Yes.
42
Originally posted by Chris Wox:
I checked this we are only allowed to do below for local variable and for
field variables we must initialized the value meaning blank final is only applicable for local variables ... java v 1.4
final int x ;
[ May 26, 2006: Message edited by: Chris Wox ]
Originally posted by Jeroen T Wenting:
Global to a classloader is bad enough. When people then also expect (as many do) it to be global across classloaders when it isn't it goes downhill from there and you (as the initial programmer) have something you think is global but isn't and it's for the maintenance programmer to figure out what the problem is and fix it which may well turn out to be a rather expensive rewrite.
Almost as bad as the scenario where you have a global that you think isn't and gets changed somewhere outside your control...
Tony Morris
Java Q&A (FAQ, Trivia)
Originally posted by Ken Blair:
You seem to be missing the distinction between a final being intialized somewhere and it being initialized with a variable initializer in it's declaration. Of course all final fields must be initialized but they don't have to be initialized in their declaration. When the initialization occurs elsewhere it is a blank final.
42
Originally posted by Jeroen T Wenting:
try twisting words Tony, you won't get smart people to admit that everything is global or that having everything accessible to all the world to do with as they please is a smart thing to do.
Tony Morris
Java Q&A (FAQ, Trivia)
"I'm not back." - Bill Harding, Twister
Tony Morris
Java Q&A (FAQ, Trivia)
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |