fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. interface Calc {
  8. int calc();
  9. default void println() {
  10. System.out.println(this.getClass().getSimpleName() + ": " + calc());
  11. }
  12. }
  13.  
  14. class SomeClass {
  15.  
  16. private int x;
  17.  
  18. public SomeClass(int x) {
  19. this.x = x;
  20. }
  21.  
  22. public class InnerSum implements Calc {
  23. private int y;
  24. public InnerSum(int y) {
  25. this.y = y;
  26. }
  27. public int calc() {
  28. return x + y;
  29. }
  30. }
  31.  
  32. public static class Nested implements Calc {
  33. private int y;
  34. public Nested(int y) {
  35. this.y = y;
  36. }
  37. public int calc() {
  38. return y; // x is not available here
  39. }
  40. }
  41. }
  42.  
  43. /* Name of the class has to be "Main" only if the class is public. */
  44. class Ideone {
  45.  
  46. public static void main (String[] args) throws java.lang.Exception {
  47. SomeClass a = new SomeClass(12);
  48. Calc ai = a.new InnerSum(34);
  49. Calc an = new SomeClass.Nested(34);
  50. ai.println();
  51. an.println();
  52. }
  53. }
Success #stdin #stdout 0.1s 320512KB
stdin
Standard input is empty
stdout
InnerSum: 46
Nested: 34