?
Solved

how can i initialize public static final variable in a function..

Posted on 2005-05-02
17
Medium Priority
?
215 Views
Last Modified: 2010-03-31
i have a field ref_no which is public static final in class ABC, this field needs to retains its value  ,and i get this value only in one of my fucntions func() of class ABC,how can i set this variable through this fucntion of the class..i am online ,it is very very urgent,i am giving 500 points
0
Comment
Question by:DilrajSingh123
  • 5
  • 5
  • 5
  • +1
17 Comments
 
LVL 5

Expert Comment

by:steve_bagnall
ID: 13907952
Hi DilrajSingh123,
If you've defined it as being public static final you are stating that it cannot be changed (the "final" modifier), so there is no way to change this variable.

Cheers,
Steve
0
 

Author Comment

by:DilrajSingh123
ID: 13907964
but i want to set one variable that i will use as public static final as this is coming from a url..do you have some other way
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13907969
Make it public static
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:DilrajSingh123
ID: 13907975
is there a way by keeping it private
0
 
LVL 5

Accepted Solution

by:
steve_bagnall earned 1400 total points
ID: 13907978
private static
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13907979
>> is there a way by keeping it private
Use a setter and a getter
0
 

Author Comment

by:DilrajSingh123
ID: 13907982
is this for beans,i think yes ,i am not using beans
0
 
LVL 5

Expert Comment

by:steve_bagnall
ID: 13907985
doesn't matter you can stikll define getter and setter methods
0
 
LVL 5

Expert Comment

by:steve_bagnall
ID: 13907988
private static String test;

String getTest() {
 return test;
}

void setTest(String test) {
 this.test = test;
}
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 600 total points
ID: 13907990
And to avoid the setter is called twice you could do something like:

private int refNo = -1;

public void setRefNo(int refno) {
    if (refNo==-1)
       refNo = refno;
}

public int getRefNo() {
   return refNo;
}
0
 

Author Comment

by:DilrajSingh123
ID: 13908006
this int might work but does not look a good practice
0
 
LVL 5

Expert Comment

by:steve_bagnall
ID: 13908018
when you set an int to -1 it is called a "sentinel" value and is quite normal, especially in the old days ;)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13908023
Why do you want it to be public static final?

>> this field needs to retains its value  ,and i get this value only in one of my fucntions func() of class ABC

Do you mean that it will be assigned its initial value from the func () method and thereafter it will retain the same value (i.e., it will never be changed after that)? If so, you could use a getter/ setter this way (without beans):

private static int refNo = 0 ;

public static int getRefNo ()
{
  return refNo ;

}

public static void setRefNo ( int theRefNo )
{
  if ( refNo != 0 )
    throw new RuntimeException ( "Ref-number is already set. " ) ; // end if

  else
    refNo = theRefNo ; // end else

}

The field remains private and you can set its value only once (I am assuming that 0 is not a valid value for that field. You can set whatever value you want as the initial incorrect value).
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13908028
>> does not look a good practice
Can you explain why not?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13908042
AAAAaaaarrrrgggghhhh....
0
 

Author Comment

by:DilrajSingh123
ID: 13908096
i think this is personal preference,for me "private static did its job " as i wanted to retain the value,and give this value through a fucntion,i apperciate zzynx for help and i definitly learnt from him,so i will divide the point between zzynx(150) and steve bagnall(350) ,more to steve bagnall as his solution is working for me without putting extra effort.
Thanks
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13908125
Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question