Solved

final variable

Posted on 2011-03-25
5
323 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Systems talking to each other 5 126
@SBGen Method 3 38
eclipse shortcuts 9 45
diffSum example 4 26
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…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

895 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

12 Experts available now in Live!

Get 1:1 Help Now