aspose file tools*
The moose likes Beginning Java and the fly likes Static & Final modifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static & Final modifiers" Watch "Static & Final modifiers" New topic
Author

Static & Final modifiers

Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Final modifier is used in a member variable to declare constants.
Static modifier when declared says that there is only one copy of the variable shared across the instances.


But my question here is what advantage do we get in declaring a variable as static & final. Where it can be used.


[ March 01, 2005: Message edited by: Srinivasa Raghavan ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489


Final modifier is used in a member variable to declare constants.
Static modifier when declared says that there is only one copy of the variable shared across the instances.


Yes, but declaring a variable static and non-final (no such keyword) means that the variable value could still be changed, right ?



Are you asking if all final variables shouldn't be by default static too ?

ram.
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Just got confused , yep it's clear now.Thanks dude.

I have one more question.

On declaring a variable as static & final with out initilizing it a value like this.



It gets automatically initilized to "0" [ Exclude the "" ], but a non static final variable doesn't, a developer is allowed to initilize it before the C'tor ends. Is there any specific reason for this default initilization behaviour in case of "static final "variable.
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489


On declaring a variable as static & final with out initilizing it a value like this.

code:
----------------------------------------------------------------------------

public static final int SOME_VAR;

----------------------------------------------------------------------------
It gets automatically initilized to "0" [ Exclude the "" ]


funny, my take is that it would throw a compile error.
Checked it, my ide complains (The blank final field SOME_VAR may not have been initialized)


but a non static final variable doesn't, a developer is allowed to initilize it before the C'tor ends.


correct. Pls look at my previous post. If this want possible, wouldnt all final variables have been static by default ?

ram.
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Yesterday when i was working in my home i face this kind of error.
May be i'm wrong. I'll post the exact code that throwed the error by tomorrow.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
The main difference between a blank final instance variable and a blank final static variable is that a blank final instance variable can be initialized by an initializer block or a constructor while a blank final static variable can only be initialized by a static initializer block.

It is true that a blank final variable has a default value before it is initialized. This can be seen by printing the variable before initializing it but after the declaration appears in the source code. Nonetheless, the blank final variable must be initialized or you get a compiler error.

From the Java Language Specifications:

8.3.1.2 final Fields
A field can be declared final (�4.5.4). Both class and instance variables (static and non-static fields) may be declared final.

It is a compile-time error if a blank final (�4.5.4) class variable is not definitely assigned (�16.7) by a static initializer (�8.7) of the class in which it is declared.

A blank final instance variable must be definitely assigned (�16.8) at the end of every constructor (�8.8) of the class in which it is declared; otherwise a compile-time error occurs.


Mike Gershman
SCJP 1.4, SCWCD in process
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static & Final modifiers