final variable ( constants)

Hello everyone,

I have declared a variable to be a constant like this:

public class example
{
public static final String str="Hello";

public static void main......

}

This sounds really strange but I am able to change str in the main program. I thought final makes a variable const

Thanks


th
zizi21Asked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
It's the reference that's final not the contents. Try

num = new String[]{};
0
 
CEHJCommented:
>>but I am able to change str in the main program.

Shouldn't be able to. Please post code where you change it
0
 
objectsCommented:
you're right, you shouldn't be able to.
Are you sure you're not changing the value of another variable with the same name.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
objectsCommented:
eg.

public class example
{
public static final String str="Hello";

public static void main......
{
    String str = "dddd";

}
0
 
zizi21Author Commented:
public class example
{
public static final String[] num={"One", "Two", "Three"};

public static void main(String []arg)
{

num[0]="Change";

}
}

Hi,

I am learning how to use final . So, the program actually does nothing..But I am able to change num[0] to another value..
The compiler didn't give any compilation error with javac ..
Thanks

0
 
objectsConnect With a Mentor Commented:
you're not changinmg the value of 'num', you're changing the value of the objects *in* the array
0
 
CEHJCommented:
IOW you can't change *the reference*
0
 
objectsCommented:
you can do that, you cannot do:

num = .......
0
 
objectsCommented:
see the last para here

http://mindprod.com/jgloss/final.html
0
 
zizi21Author Commented:
thanks for the fast reply. Really appreciate this..

If I understand this correctly, I could change content because the reference is final and not the content..

Is there a way to make the content const ? If there isn't, since the content is changeable, am I assuming right that strings and arrays of strings should not be declared as final as it doesn't make much difference.

Thanks.
0
 
objectsCommented:
strings is fine (they are immutable)
0
 
zizi21Author Commented:
Pls give me a sec..Going through the links..
0
 
objectsCommented:
not as much value in final arrays, but can still be usefiul sometimes. helps the compiler out a little too.
0
 
objectsCommented:
for example, having a final arrays stops its size being changed.
0
 
zizi21Author Commented:
I get it, we usually declare as final to expediate the compilation time..
Does it make much difference, if I just declare it in the public static void main instead of the at the class

public class example{

public static void main(String [] args)
{
final String[]... (except of not having the word static in here ....

}

Is there way a common practice if there is only one class..thanks

0
 
zizi21Author Commented:
typo..what i meant to say was that is it a common practice to declare final variables (without the word static) in the int main..
0
 
CEHJCommented:
>>that is it a common practice to declare final variables (without the word static) in the int main..

final variables are usually seen where constants are needed
0
 
CEHJCommented:
:-)
0
 
zizi21Author Commented:
but do we declare it outside the main or inside. let me know if i am confusing..
for example:
a)
public static final int a=5;
public static void main()....

b) public static void main()
fnal int a=5;

0
 
CEHJCommented:
The 'finality' is not affected by where it's declared. Position only affects its accessibility
0
 
zizi21Author Commented:
thanks:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.