• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what is the use of creating object as a final?.

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
what is the difference b/w final objects and normal objects.

public class A
{
........
.........
.........

}

final A a=new A();
A b=new B();

what is difference b/w a & b;


Thanks and Regards
Madhu
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Other than the fact that A and B are different classes?

A "final" variable is one that cannot be changed after being initialized. Note that this means the object that the variable refers to can't be changed, but the object's member variables may themselves be changed.

Please don't post stuff like this in the "Advanced" forum. Moving to Java in General (Beginner.)
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
final A a=new A();
A b=new B();

You can assign a value to a final variable once. That is the difference. After you last line of code you could say b = a but you'll get a compiler error if you try a = b.

Because a is already initialized. Therefore you declare a variable as final when you would like to avoid programming errors like replacing the value of a variable with another.

The final keyword, when apply to an object variable will not avoid, however, that the state of a mutable object is changed.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Notice that there is no such thing as a final *object*. What is final in your example is the *reference* to the object, not the object itself (which is an important distinction).
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic