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 printsrocky here
- then call
start()
printsrocky 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 invokerun
).- if override
thread.start
own method, there's no magic left anywhere.
- if override
Comments
Post a Comment