Solved

final variable

Posted on 2011-03-25
5
322 Views
Last Modified: 2012-05-11
Hi,

I read
http://www.jchq.net/certkey/0102certkey.htm
as

A final variable cannot have it's value changed and must be set at creation time(by setters and getter).  I was not clear on that. Any ideas, resources, links, sample code highly appreciated. thanks in advance.
0
Comment
Question by:gudii9
5 Comments
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 35220306
You cannot change the value of a final variable once it has been set.
Think of it like a constant

If you have a final var:

private final int x = 6;

Then you cannot later change its value

x = 7;    // will not compile
0
 
LVL 20

Assisted Solution

by:Sathish David Kumar N
Sathish David  Kumar N earned 125 total points
ID: 35221412
you cant reassign the values thats the best word ....

i have the same question long time back please refer my question

http://www.experts-exchange.com/Programming/Languages/Java/Q_26795127.html
0
 

Assisted Solution

by:Mantas Brasiunas
Mantas Brasiunas earned 125 total points
ID: 35221515
You can't change the value of final variable. It's like a constant.

Take a look at this class:

public class FinalVariableExample {
      private final int number_ = 6;
      
      public FinalVariableExample() {}

      public int getNumber_() {
            return number_;
      }
      
      public void setNumber_(int number) {
            number_ = number; // You will get error here.
      }
}
0
 
LVL 3

Assisted Solution

by:dileepan_k
dileepan_k earned 125 total points
ID: 35226099
It is the variable's reference that cannot be modified. However, the heap that it is pointing can be altered through the variable itself. for example

final StringBuilder sb = new StringBuilder("abc");
sb.Append ("def");

but

sb = new StringBuilder("123");

is not allowed.
0
 
LVL 7

Author Comment

by:gudii9
ID: 35234563
>>>It is the variable's reference that cannot be modified. However, the heap that it is pointing can be altered through the variable itself. for example


can you please elaborate it. I have not understood clearly
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now