How method preference work in java? -


i want understand how below code snippet work ?

class annathread extends thread {      public static void main(string args[]){         thread t = new annathread();         t.start();      }      public void run(){         system.out.println("anna here");     }     public void start(){         system.out.println("rocky here");     } } 

output - rocky here

there's not explain.

  • you override start() code prints rocky here
  • then call start() prints rocky here.
  • (the run method never involved)

people confuse purpose of start , run. see instance question:

      why call thread.start() method in turns calls run method?

the rules simple:

  • thread.run ordinary method (no magic)

  • thread.start contains magic because spawns separate thread (and lets thread invoke run).

    • if override thread.start own method, there's no magic left anywhere.

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -