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
runmethod 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.runordinary method (no magic)thread.startcontains magic because spawns separate thread (and lets thread invokerun).- if override
thread.startown method, there's no magic left anywhere.
- if override
Comments
Post a Comment