+ 1

What does this code mean ?

Handler myHandler = new Handler(); myHandler.post(new Runnable() { //some code }); My question is what's "new Runnable()" here ? Is it a nameless object of class Runnable ? Can someone break this code down , like create a real object from Runnable() and call that object in post () method by name.

14th Dec 2020, 7:06 AM
Mons Joseph
Mons Joseph - avatar
3 Answers
+ 1
Mons Joseph Runnable is an interface not a class but we can create an object of interface like in this case new Runnable () { } But there is a difference in object creation of class and interface. In an interface we need { } block to implement all methods but in an object of class there is no { }. There is paranthesis () which used to pass the arguments in method.
14th Dec 2020, 8:24 AM
A͢J
A͢J - avatar
+ 2
Looks somehow related to this too: https://www.sololearn.com/learn/Java/2172/ I'll leave further explanations for the experts. Edit: P.S: Can you share the full code, maybe give some more context to it?
14th Dec 2020, 7:24 AM
Kevin ★
+ 1
Runnable is an interface to create threads. https://www.sololearn.com/learn/Java/2185/
14th Dec 2020, 7:10 AM
你知道規則,我也是
你知道規則,我也是 - avatar