?
Solved

final variable

Posted on 2011-03-25
5
Medium Priority
?
332 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 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 500 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 500 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 500 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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month10 days, 4 hours left to enroll

762 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