Solved

final variable

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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

726 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