Solved

final variable

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 64
Spring Framework HTTPSession management 1 45
servlet filter example 37 72
hibernate insert example 13 34
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

829 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