KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Please check the Java code below: public class Test { public static void main(String arg[]) throws Throwable { Test t = new Test(); System.out.println(t.meth().s); //OP: Old value System.out.println(t.meth().getVal()); //OP: String Implementation } private TestInter meth() { return new TestInter() { public String s = "String Implementation"; public String getVal() { return this.s; } }; } } interface TestInter { String s = "Old value"; String getVal(); } As you see I have created an interface anonymously. When I access the interface variable directly, it will show "Old value". t.meth().s => "Old value" Accessing it through the getVal() method returns proper values, t.meth().getVal() => "String Implementation" I don't understand how this code works, can somebody explain it to me?
Tags (comma-separated)
Save Edits
Cancel