Skip to content

Instantly share code, notes, and snippets.

@pvorb
Last active December 16, 2015 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pvorb/5349962 to your computer and use it in GitHub Desktop.
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
Copy link
Author

pvorb commented Apr 9, 2013

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