Skip to content

Instantly share code, notes, and snippets.

@pvorb
Last active December 16, 2015 00:48
Show Gist options
  • Select an option

  • Save pvorb/5349962 to your computer and use it in GitHub Desktop.

Select an option

Save pvorb/5349962 to your computer and use it in GitHub Desktop.
Equivalent to object literals in Java
public class ObjLiteralsTest {
public void main(String[] args) {
Object user = new Object() {
int id = 1;
String name = "pvorb";
};
// user.id and user.name cannot be accessed,
// making the whole construct useless though
//
// System.out.println(user.id, user.name)
// will not compile
}
}
@pvorb

pvorb commented Apr 9, 2013

Copy link
Copy Markdown
Author

This Gist exists to prove that you can create Objects directly in Java. You cannot access it's properties though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment