• Post Reply Bookmark Topic Watch Topic
  • New Topic

String and Final  RSS feed

 
Bhagat Singh Rawat
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rancher,

Can someone explain the below mentioned scenarios in the detail?

Scenrio-1

Scenario-2

If String class is final class then why do we need to create explicitly a final String Instance for CONSTANT variables?

Thanks in advance.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a difference between a class being final and a variable being final.

Final for a class means that it can't be subclassed. Nothing more, nothing less.
Final for a variable (local, instance field, static field) means that it can only can have its value set once - and never again. A final parameter is similar - it can only be assigned from the method call.


In your example, you first assign "Test" to the final variable name. That means that name can never get a new value, and that's just what you're trying to do afterwards.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!