tag:blogger.com,1999:blog-8712770457197348465.post3599895818057796323..comments2024-03-28T02:47:19.159-07:00Comments on Javarevisited: What is final in Java? Final variable , Method and Class Examplejavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger22125tag:blogger.com,1999:blog-8712770457197348465.post-82393540172072727802017-07-14T11:11:28.570-07:002017-07-14T11:11:28.570-07:00The common perception is that declaring classes or...The common perception is that declaring classes or methods final makes it easier for the compiler to inline method calls, but this perception is incorrect (or at the very least, greatly overstated).<br />{Taken from: https://www.ibm.com/developerworks/java/library/j-jtp1029/index.html }Suraj Tamanghttps://www.blogger.com/profile/15846878093833723418noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-62649888776381735882016-07-23T11:52:41.623-07:002016-07-23T11:52:41.623-07:00what about final Object with some member functions...what about final Object with some member functions. If you make an Object of a non-final class final then How will it behave what sort of member function will it be able to invoked and what will it restrain remember the class is non-final that is regular classAnonymoushttps://www.blogger.com/profile/17497254695991412996noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-41196525895109453512016-01-18T04:10:42.771-08:002016-01-18T04:10:42.771-08:00@Sehsh, you are right, that was typo, variable nam...@Sehsh, you are right, that was typo, variable name is loans there not list. Corrected it now.javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-21655576823563524622016-01-18T01:15:01.763-08:002016-01-18T01:15:01.763-08:00private final List Loans = new ArrayList();
list.a...private final List Loans = new ArrayList();<br />list.add(“home loan”); //valid<br />list.add("personal loan"); //valid<br />loans = new Vector(); //not valid<br />In the above code snippet the name of the variable should be list instead of Loans... as per my knowledgeAnonymoushttps://www.blogger.com/profile/13220494735013309575noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-84418024884319870242016-01-05T04:14:04.829-08:002016-01-05T04:14:04.829-08:00@Anonymous, good catch. That's right, the coll...@Anonymous, good catch. That's right, the collection should be Loans there.javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-30082994740134080762016-01-05T01:20:26.740-08:002016-01-05T01:20:26.740-08:00Nice article.. very much useful! Thank you! btw......Nice article.. very much useful! Thank you! btw....Last example, it should be corrected as follows<br />Loans.add("home loan"); //valid<br />Loans.add("personal loan"); //validAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-60407692915470868852015-07-06T02:26:24.812-07:002015-07-06T02:26:24.812-07:00Final object references
The fields on any object a...Final object references<br />The fields on any object accessed via a final reference are also guaranteed to be at least as up to date as when the constructor exits. This means that: <br /><br />Values of final fields, including objects inside collections referred to by a final reference, can be safely read without synchronization.<br />Note that if you have a final reference to a collection, Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-58640104414626611252014-06-22T15:46:47.408-07:002014-06-22T15:46:47.408-07:00For the last example, shouldn't it be "Lo...For the last example, shouldn't it be "Loans.add(“home loan”); <br />Loans.add("personal loan");" ?codingdoghttps://www.blogger.com/profile/00625648567791335563noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-29799321640695628232014-03-25T18:45:37.716-07:002014-03-25T18:45:37.716-07:00One more thing to note about final variables in Ja...One more thing to note about final variables in Java is that, till Java 7, you can not use a non final local variable inside anonymous class, but from Java 8 you can. JDK 8 introduced a concept called effective final, a variable is considered effective final if it is not modified after initialization in local block. What this means is you can now use local variable without final keyword inside Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-82303268374880070582013-11-25T03:19:48.705-08:002013-11-25T03:19:48.705-08:00"You can not make a class immutable without m..."You can not make a class immutable without making it final and hence final keyword is required to make a class immutable in java"<br /><br />I don't agree with this statement, a very simple example would be:<br /> - a final class can still be mutable if its fields are non-final and are not-private or have setters available for them<br /> - similarly a non-final class can still be Anonymoushttps://www.blogger.com/profile/06536449989922417074noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-58723785469803662662013-09-04T09:51:02.679-07:002013-09-04T09:51:02.679-07:00One Suggestion
You had cover everything about fina...One Suggestion<br />You had cover everything about final here. One thing which as if i know is missing is about final instance variable,final static variable and final local variable (which you cover) missed is about the default values what we get for instance and static variable we will not get for final instance and final static variables , where we can initialize them in constructor and Sudhahttps://www.blogger.com/profile/00672427632288538158noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-78491832779214677522013-08-21T10:19:32.436-07:002013-08-21T10:19:32.436-07:00Can you provide the solution of this Q??
Q:As we k...Can you provide the solution of this Q??<br />Q:As we know that if at any point some exception occurs in the code then an instance of Exception class (or its subclass) is thrown from that point. So my question is the instantiation comes into picture at runtime while checked exception are checked at compile time , therefore no instantiation is there, so how compiler comes to know that it is a Anonymoushttps://www.blogger.com/profile/08565524934583279908noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-32189080258972467112013-07-11T02:00:35.538-07:002013-07-11T02:00:35.538-07:00Can you please provide some examples of final meth...Can you please provide some examples of final methods from Java API? I am learning final keywords and want to know more about, how to use final with methods.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-37540039009753343482013-04-24T05:51:30.863-07:002013-04-24T05:51:30.863-07:00> "Local final variable must be initializi...> "Local final variable must be initializing during declaration."<br /><br />This doesn't seem to be correct? For example, the following snippet compiles and runs okay under Java 1.7.0:<br /><br />final int f; // Declare a final local variable<br />System.out.println(new java.util.Date()); // Do some other things<br />f = 5; // Initialize the final variable<br />Jon Schneiderhttps://www.blogger.com/profile/05718316809087214442noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-55207934030940918352013-03-04T20:13:30.623-08:002013-03-04T20:13:30.623-08:00Can you please suggest when to use final methods i...Can you please suggest when to use final methods in Java, what are best practices around using final. I mean, I always get confused, should I make a class final or not, is there a rule book which we can follow? I hope my IDE suggest this but not so far.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-84623571433450803612013-01-07T09:06:47.754-08:002013-01-07T09:06:47.754-08:00A blank final, means that it's not initialized...A blank final, means that it's not initialized explicitly at it's declaration, can only be initialized in an initialization block or a constructor. A reference, declared final, doesn't make the object final.Shirinhttps://www.blogger.com/profile/13626183449915970990noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-53631705438338860012012-09-12T20:50:56.218-07:002012-09-12T20:50:56.218-07:00What is blank final variable in Java ? is it manda...What is blank final variable in Java ? is it mandatory to initialize blank final variable in Constructor ? What is my class have multiple constructor, do I need to initialized blank final variable in all constructor or is there any alternative of that ?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-62325058884991241352012-08-27T19:45:11.168-07:002012-08-27T19:45:11.168-07:00Making a class final is double edged sword, Some J...Making a class final is double edged sword, Some Java programmer argue that final class severely limits client's ability to inherit and extend while Some Java programmer are in opinion that final class, which is Immutable are best way to design robust System.It's trade-off. James Gosling suggest that Making a class final in Java for security reason e.g. String can be justified.POJOnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-9028649884376653812012-06-10T17:38:47.509-07:002012-06-10T17:38:47.509-07:00Race conditions occurs when two thread operate on ...Race conditions occurs when two thread operate on same object without proper synchronization and there operation interleaves on each other. Classical example of Race condition<br /><br />Read more: http://javarevisited.blogspot.com/2012/02/what-is-race-condition-in.html#ixzz1xRNIKEnVravinoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-43088252198335056132012-01-12T09:55:09.324-08:002012-01-12T09:55:09.324-08:00Can final variables cloned along with object ?Can final variables cloned along with object ?Santunoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-42963987117741695862012-01-09T01:59:25.887-08:002012-01-09T01:59:25.887-08:00@ Michee - Immutable approximately means the same ...@ Michee - Immutable approximately means the same as a final. Something that cant be changed. But, in Java, the term immutable is usually used along with String objects when we say Java String objects are Immutable. <br /><br />Anand<br /><a href="http://inheritingjava.blogspot.com" rel="nofollow">Inheriting Java</a>Anand Vijayakumarhttps://www.blogger.com/profile/04069290912727901147noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-43927879224501341662011-12-31T03:16:39.449-08:002011-12-31T03:16:39.449-08:00what's the difference between final and immuta...what's the difference between final and immutable?<br /><br />Great blog!<br />Happy New Year!micheehttps://www.blogger.com/profile/05937230965190972040noreply@blogger.com