Tom,
The static and final modifiers are quite different.
static:
1) The variable belongs to the class so you don't even have to create an
instance (object) of the class to reference the variable.
2) Since the variable belongs to the class, every object that is created
will reference that same variable.
3) A static variable can be changed. But, per #2 above - every instance of
the class (i.e. every object) will see the change since the variable
belongs to the class and not to the object.
For a more technical description, see the Java Language Reference (scroll
down to section 8.3.1.1 static variables). There is also a (not very
useful) description of the final modifier in section 8.3.1. Keep reading
for details about the final modifier.
http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78091
-----------------------------
final:
1) A final variable cannot be changed so to answer your question, "Yes,
final variables can be used as constants". BUT, please see #2 below. You
might want to consider "public final static" or at least "final static" for
constants.
2) If you create a final variable and assign an object to it, you need to
remember that the contents of the object CAN be changed. For example, if
you create a final variable and assign an array to it, you can change the
contents of the array but the variable will always refer to the same array.
For the definitive word on this, see the Java Language Reference (section
4.5.4 final variables):
http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#10931
-----------------------------
public final static:
Useful for defining constants because:
1) Everyone has access to it (public).
2) Cannot be changed (final).
3) It is available before an instance of the class is created (static).
See the java.awt.Font class for examples such as java.awt.Font.BOLD. You
can use java.awt.Font.BOLD without creating a Font object. The
documentation for java.awt.Font can be found at the link below:
http://java.sun.com/j2se/1.3/docs/api/java/awt/Font.html
- Todd Chaffee
At 08:43 AM 11/08/00 -0500, you wrote:
>This is a newbee question: What is the difference between static and final
>when describing a field?
>
>It seems to me from what I've read that they both describe a field as
>constant.
>
>Thanks,
>Tom T
>
>
>+---
>| This is the JAVA/400 Mailing List!
>| To submit a new message, send your mail to JAVA400-L@midrange.com.
>| To subscribe to this list send email to JAVA400-L-SUB@midrange.com.
>| To unsubscribe from this list send email to JAVA400-L-UNSUB@midrange.com.
>| Questions should be directed to the list owner: joe@zappie.net
>+---
+---
| This is the JAVA/400 Mailing List!
| To submit a new message, send your mail to JAVA400-L@midrange.com.
| To subscribe to this list send email to JAVA400-L-SUB@midrange.com.
| To unsubscribe from this list send email to JAVA400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner: joe@zappie.net
+---