fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.lang.reflect.Constructor;
  7. import java.lang.reflect.Modifier;
  8.  
  9.  
  10. abstract class Foo {
  11. protected int x;
  12.  
  13. protected Foo(int x) {
  14. this.x = x;
  15. }
  16.  
  17. public abstract void bar();
  18. }
  19. /* Name of the class has to be "Main" only if the class is public. */
  20. class Ideone
  21. {
  22. public static void main (String[] args) throws java.lang.Exception
  23. {
  24. Foo foo = new Foo(123) { // <<== This works because of "compiler magic"
  25. public void bar() {
  26. System.out.println("hi " + x);
  27. }
  28. };
  29. foo.bar();
  30.  
  31. Class<?> clazz = foo.getClass();
  32. for (Constructor<?> c : clazz.getDeclaredConstructors()){
  33. System.out.println(c);
  34. System.out.println("public? "+Modifier.isPublic(c.getModifiers()));
  35. System.out.println("protected? "+Modifier.isProtected(c.getModifiers()));
  36. System.out.println("private? "+Modifier.isPrivate(c.getModifiers()));
  37. // System.out.println("default? "+Modifier.isProtected(c.getModifiers()));
  38. }
  39. }
  40. }
Success #stdin #stdout 0.06s 380160KB
stdin
Standard input is empty
stdout
hi 123
Ideone$1(int)
public? false
protected? false
private? false