Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > a question related to "final static" field variable

Reply
Thread Tools

a question related to "final static" field variable

 
 
www
Guest
Posts: n/a
 
      05-04-2007
Hi,

I want to have a static field variable. I want to initiate it once and
don't want it to be changed any more. So

public MyClass
{
public final static rootDir; //error: saying rootDir may not be
initialized

public MyClass(String dir)
{
rootDir =dir; //error: saying that I cannot assign to a final variable
}

...

public void doIt()
{
...
}

public static void main(String[] args)
{
String str = args[0];
MyClass class = new MyClass(str);

}
}

I thought final variable can be assigned value once and only once. And
this can be done inside constructor. Cited from this web page:
http://renaud.waldura.com/doc/java/final-keyword.shtml in Final Field part.

Thank you for your help.
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      05-04-2007
www wrote:
> Hi,
>
> I want to have a static field variable. I want to initiate it once and
> don't want it to be changed any more. So
>
> public MyClass
> {
> public final static rootDir; //error: saying rootDir may not be
> initialized
>
> public MyClass(String dir)
> {
> rootDir =dir; //error: saying that I cannot assign to a final
> variable
> }
>
> ...
>
> public void doIt()
> {
> ...
> }
>
> public static void main(String[] args)
> {
> String str = args[0];
> MyClass class = new MyClass(str);
>
> }
> }
>
> I thought final variable can be assigned value once and only once. And
> this can be done inside constructor. Cited from this web page:
> http://renaud.waldura.com/doc/java/final-keyword.shtml in Final Field part.
>
> Thank you for your help.


Unfortunately not if it is also static.

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
 
 
 
visionset
Guest
Posts: n/a
 
      05-04-2007

"www" <(E-Mail Removed)> wrote in message
news:f1ftq1$co1$(E-Mail Removed)...

> I want to have a static field variable. I want to initiate it once and
> don't want it to be changed any more. So
>
> public MyClass
> {
> public final static rootDir; //error: saying rootDir may not be
> initialized
>...
> I thought final variable can be assigned value once and only once. And
> this can be done inside constructor. Cited from this web page:
> http://renaud.waldura.com/doc/java/final-keyword.shtml in Final Field
> part.
>
> Thank you for your help.


The problem is that the rootDir reference may be accessed before the
constructor is called since it is static.
The obvious value for it to hold would be null, so you therefore would not
be able to reasign in your constructor. Because of this the compiler forces
you to assign at the point of declaration.

--
Mike W


 
Reply With Quote
 
Sanjay
Guest
Posts: n/a
 
      05-04-2007

> I thought final variable can be assigned value once and only once. And


It is a static variable and has nothing to do with the object of that
class. It get its value when classloader loads the class. What you are
trying to do is, not assigning value to 'rootDir' at all and then are
trying to change it in the contructor, hence the error.

> this can be done inside constructor. Cited from this web page:
> http://renaud.waldura.com/doc/java/final-keyword.shtml in Final Field

part.

I am not sure what that web page says, but there is a subtle difference
between final and final static variable. May be you want to read this

http://java.sun.com/docs/books/tutor...classvars.html
 
Reply With Quote
 
www
Guest
Posts: n/a
 
      05-04-2007
Thank you all. I have realized I was wrong. But, to achieve my purpose,
do you have any suggestions?

Thank you.
 
Reply With Quote
 
Roman
Guest
Posts: n/a
 
      05-04-2007
www pisze:
> Hi,
>
> I want to have a static field variable. I want to initiate it once and
> don't want it to be changed any more. So
>
> public MyClass
> {
> public final static rootDir; //error: saying rootDir may not be
> initialized
>
> public MyClass(String dir)
> {
> rootDir =dir; //error: saying that I cannot assign to a final
> variable
> }
>
> ...
>
> public void doIt()
> {
> ...
> }
>
> public static void main(String[] args)
> {
> String str = args[0];
> MyClass class = new MyClass(str);
>
> }
> }
>
> I thought final variable can be assigned value once and only once. And
> this can be done inside constructor. Cited from this web page:
> http://renaud.waldura.com/doc/java/final-keyword.shtml in Final Field
> part.
>
> Thank you for your help.

you cannot assign value to final variable in an any method (even
constructors). You have to decide: final or assigne value during
declaration.
Regards,
Roman
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      05-04-2007
www wrote:
> Thank you all. I have realized I was wrong. But, to achieve my purpose,
> do you have any suggestions?
>
> Thank you.


Make rootDir private and change it only once inside the class, from main.

If other classes need access you can provide a getRootDir() method,
which can check for attempts to read it before it has been set.

Patricia
 
Reply With Quote
 
Tom Hawtin
Guest
Posts: n/a
 
      05-04-2007
www wrote:
>
> I want to have a static field variable. I want to initiate it once and
> don't want it to be changed any more. So


> public final static rootDir; //error: saying rootDir may not be

^String
> initialized


> public static void main(String[] args)
> {
> String str = args[0];
> MyClass class = new MyClass(str);


It doesn't really make sense for this kind of variable to be static. Any
code that uses the variable, directly or indirectly, is specifying the
interpretation of class loader scope. Stick with instance variables, and
keep dependencies as narrow as possible.

Use static fields judiciously. They are good for constants[1], really
difficult for caches and hopeless for practically anything else.

Tom Hawtin

[1] Constants as in things that cannot change, not just as in the JLS
"compile-time constant".
 
Reply With Quote
 
visionset
Guest
Posts: n/a
 
      05-04-2007

"Roman" <(E-Mail Removed)> wrote in message
news:f1g0k5$c8a$(E-Mail Removed)...

>> I thought final variable can be assigned value once and only once. And
>> this can be done inside constructor. Cited from this web page:
>> http://renaud.waldura.com/doc/java/final-keyword.shtml in Final Field
>> part.
>>


> you cannot assign value to final variable in an any method (even
> constructors). You have to decide: final or assigne value during
> declaration.


That is not true, a final instance variable (attribute) can be assigned
inline or in the constructor.

--
Mike W


 
Reply With Quote
 
www
Guest
Posts: n/a
 
      05-04-2007
Patricia Shanahan wrote:

>
> Make rootDir private and change it only once inside the class, from main.
>
> If other classes need access you can provide a getRootDir() method,
> which can check for attempts to read it before it has been set.
>
> Patricia


Suppose my program has about 20 classes and the class with main method
is MyClass provided in my original posting. Suppose one class
"Worker.java" needs to know the root directory in order to read in the
text file. Right now, Worker does NOT have a MyClass in it. But, if I
follow your method, I need:
<inside Worker.java>
MyClass class = new MyClass();
String rootDir = class.getRootDir();
....//append rootDir in the front of text file name

</inside Worker.java>

I feel it is kind of silly that in order to get to know what the root
directory it is, Worker is forced to have a MyClass in it.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript validation for a not required field, field is onlyrequired if another field has a value jr Javascript 3 07-08-2010 10:33 AM
question related to passing variable number of arguments junky_fellow@yahoo.co.in C Programming 8 09-20-2007 05:43 AM
1.Enter space bar for field names and save the field.The field shoud not get saved and an alert should be there as"Space bars are not allowed" Sound Javascript 2 09-28-2006 02:43 PM
How should multiple (related) projects be arranged (structured) and configured so that they can share code, have a related package structure and enable proper unittesting, and ensuring no namespace collisions ToddLMorgan@gmail.com Python 14 04-21-2006 04:03 PM
How should threads be terminated? (related to 'Help with thread related tracebacks') Maxwell Hammer Python 7 06-18-2005 04:20 PM



Advertisments