In C#, you can do the following:
var objResult = new { success = result };
Is there a java equivalent for this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Java does not have type inference provided to C# by the
varkeyword, so whilst you can create anonymous types they’re not much good since you can’t get at their attributes.So you can create an instance of an anonymous class like so:
But since
myobjis an instance ofObjectyou can’t accesssuccessin your code, and as you have created an instance of an anonymous class there is by definition no way to explicitly refer to this class.In C#
varsolves this by inferring the type but there is no way to do this in Java.Normally anonymous classes are used to create implementations of interfaces and abstract classes and so are referenced using the interface or parent class as the type.