Being Java programmer.
I think that needs a bit more explanation.Ganish Patil wrote:. . . final variable:
If a variable declared as final then it will become constant and we can’t perform re-assignment for that variable. Once a final variable has been assigned, it always contains the same value. . . .
yes I always fumble while explaining things & constructing english statements .Campbell Ritchie wrote:I think that needs a bit more explanation.
This only one line which is short and precise substitutes mine above two line.Campbell Ritchie wrote:A final field or local variable must be assigned to once and once only
Being Java programmer.
Campbell Ritchie wrote:A final field or local variable must be assigned to once and once only. But if the assignment is to a mutable reference type, the state of that object can be changed. That applies to arrays, since arrays are always mutable reference types.
Yes, we can use that reference to change the values of instance variables if those are not declared final but we can't point to new object. Here I wrote a program so you can understand it. Output:Quazi Irfan wrote:Do you mean we can use the reference to change the it's instance variables, but we can't point the reference to a new object?
Being Java programmer.
You got weird sequence for your class members. Declaration should follow next sequence:Ganish Patil wrote:Here I wrote a program so you can understand it.
Your welcomeQuazi Irfan wrote:Thank you
Being Java programmer.
Opps No I always follow sequence you mentioned. Didn''t notice that, Got auto getter setter methods so preceded constructor. But Thank you for noticing thatLiutauras Vilda wrote:Somehow you flipped 3 with 4, which I don't think is good.
Being Java programmer.
Jesper de Jong wrote:Ganish explained exactly what final means. It can indeed be used on classes, methods and variables and has slightly different meanings for all those cases.
Whether you should use final is also partly a matter of style.
Some people are of the opinion that you should for example make classes final unless they are designed to be subclassed. Likewise with methods - you might decide to make them final unless they are designed to be overridden in subclasses. And also with variables - when a variable is final it can't be modified, and this can make programs "safer" - you can't accidentally modify such a variable anywhere (if you try the compiler will give you an error).
It is often a good idea to make classes immutable, especially in multi-threaded programs. Writing multi-threaded programs is hard, and what especially makes it hard is if you have multiple threads that may modify the same data at the same time - you can get really difficult bugs because of the unpredictability of the order in which things happen exactly if you have two threads trying to write to the same variable. Making things immutable is one way to make multi-threaded programming easier. Making member variables final is part of what is necessary to make classes immutable, so that might be another reason why people are making variables final.
Campbell Ritchie wrote:
I think that needs a bit more explanation.Ganish Patil wrote:. . . final variable:
If a variable declared as final then it will become constant and we can’t perform re-assignment for that variable. Once a final variable has been assigned, it always contains the same value. . . .
A final field or local variable must be assigned to once and once only. But if the assignment is to a mutable reference type, the state of that object can be changed. That applies to arrays, since arrays are always mutable reference types.
I think, Liutauras Vilda also meant the sameCampbell Ritchie wrote:The order of members of a class is a style convention
Being Java programmer.
Lisa Austin wrote:...but I was looking more for the I guess "style" to when some people use it.
Sounds like good idea, will definitely try it.Campbell Ritchie wrote: make everything in sight private and final and change those modifiers only if there are problems.
Being Java programmer.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |