?
Solved

final variable ( constants)

Posted on 2007-07-28
22
Medium Priority
?
280 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:zizi21
  • 8
  • 7
  • 7
22 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 19584403
>>but I am able to change str in the main program.

Shouldn't be able to. Please post code where you change it
0
 
LVL 92

Expert Comment

by:objects
ID: 19584405
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
 
LVL 92

Expert Comment

by:objects
ID: 19584407
eg.

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

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

}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:zizi21
ID: 19584412
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 19584416
It's the reference that's final not the contents. Try

num = new String[]{};
0
 
LVL 92

Assisted Solution

by:objects
objects earned 1000 total points
ID: 19584417
you're not changinmg the value of 'num', you're changing the value of the objects *in* the array
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19584419
IOW you can't change *the reference*
0
 
LVL 92

Expert Comment

by:objects
ID: 19584420
you can do that, you cannot do:

num = .......
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19584435
0
 
LVL 92

Expert Comment

by:objects
ID: 19584441
see the last para here

http://mindprod.com/jgloss/final.html
0
 

Author Comment

by:zizi21
ID: 19584446
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
 
LVL 92

Expert Comment

by:objects
ID: 19584451
strings is fine (they are immutable)
0
 

Author Comment

by:zizi21
ID: 19584457
Pls give me a sec..Going through the links..
0
 
LVL 92

Expert Comment

by:objects
ID: 19584459
not as much value in final arrays, but can still be usefiul sometimes. helps the compiler out a little too.
0
 
LVL 92

Expert Comment

by:objects
ID: 19584468
for example, having a final arrays stops its size being changed.
0
 

Author Comment

by:zizi21
ID: 19584480
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
 

Author Comment

by:zizi21
ID: 19584483
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 19584490
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 19584493
:-)
0
 

Author Comment

by:zizi21
ID: 19584508
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 19584844
The 'finality' is not affected by where it's declared. Position only affects its accessibility
0
 

Author Comment

by:zizi21
ID: 19587172
thanks:-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month16 days, 10 hours left to enroll

862 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