-1

Is final a access modifier in java or a non access modifier ?

Is this setName() method of Thread class public final void setName(string name) really final ? If yes does it mean that once the thread name is changed then it cant be changed later because of final keyword..?

Or final is not a part of the method setName() ?

Plz guide me with the correct Answer..

Lalaboy
  • 29
  • 7
  • Another good question to read for you: http://stackoverflow.com/questions/1351568/java-basics-about-final-keyword – Erwin Bolwidt Aug 14 '16 at 05:30
  • @ErwinBolwidt Thanks alot – Lalaboy Aug 14 '16 at 05:55
  • What part of `public final void setName(String name)` don't you understand? What part of `final` don't you understand? – user207421 Aug 14 '16 at 07:13
  • @EJP I got my answer. If it has marked as final, it cant be overridden. And the predefined methods of Thread Class cant be final and if they were, we cant use it..! – Lalaboy Aug 14 '16 at 23:37

1 Answers1

0

Please understand that there is a difference between:

  1. Making member variables as final and

    • Whenever variables of any class are marked as final, it means that they can't be changed later
    • point to remember: As @GhostCat pointed out, when marking member variables as final, Once a final variable has been assigned, it always contains the same value, however, the object it refers to can possibly be changed. For example consider this List object:

      final List<Integer> list = new ArrayList<Integer>();  
      list = new ArrayList<Integer>(); // Invalid  
      list.add(2); //Valid  
      

      see this answer for more details.

  2. Marking method declarations as final

    • Whenever any method is declared as final, it means that it cant be cannot be overridden by subclasses.
  3. Making classes in java as final
    • Whenever any class is declared as final, it means that it can't subclassed, i.e., that it can't be extended by any other class. (eg, String class)

Now since the setName() method of Thread class is final doesn't mean that we can't change the name of this Thread. Instead, it simply means that this method cannot be overridden by the classes that extends this class.

Community
  • 1
  • 1
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
  • You could add information about making a class final, too. And maybe point out, that yes, the reference a final variable is pointing too can't be changed; but variable itself can still be "changed" (like: adding / removing elements when that variable is a list). – GhostCat Aug 14 '16 at 05:53
  • @Raman Sahasi Thanks a lot.! – Lalaboy Aug 14 '16 at 05:54
  • I've added that information @GhostCat Thanks for pointing it out. – Raman Sahasi Aug 14 '16 at 06:12
  • Your 'point to remember' is back to front. The variable *can't* be changed, but the object it refers to can possibly be mutated. @GhostCat's language is poorly chosen. – user207421 Aug 14 '16 at 07:15
  • @EJP I'm sorry, that was a quick copy paste. Anyways, thanks for pointing it out. I've updated my answer accordingly. – Raman Sahasi Aug 14 '16 at 08:15