+ 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.
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.
+ 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?
+ 1
Runnable is an interface to create threads.
https://www.sololearn.com/learn/Java/2185/