-1

When using reflection to inquire information about the Constructor of a class in runtime.

public class amongUs {
...
...
...
   @Retention(RetentionPolicy.RUNTIME)
   @Target({ElementType.PARAMETER, ElementType.METHOD})
   @interface Message {
   }

   public enum Colors {RED , YELLOW , GREEN ,BLACK , WHITE, BLUE, CYAN, PURPLE, VELVET, BROWN, ORANGE  
   };
   public class Player {

   }
   public class pla extends  Player {
       String color;
       String message = "im not sus";

       @Inject
       public pla(@Message Integer x,@Message String wut) {
           color = (Colors.values()[x]).name();
           message=wut;
       }

   }


...
...
}

However, when I look at the constructor of "pla" it shows it has 3 parameters.

Debugger Description

Why is it happening and how do I fix it?

  • It's an inner class. That means any instance of that class belongs to an element of the outer class, `amongUs`. You can only instantiate it using an instance of your outer class. Apparently your debugger shows that as a constructor parameter. If that's not what you intended, don't use an inner class. Use a top-level class or a static nested class. And `Player` as well. – khelwood May 19 '21 at 17:35
  • so i should make pla static? – yellowcard123 May 19 '21 at 17:36
  • Maybe. I don't know why you're using inner classes at all. Your code does not make that clear. – khelwood May 19 '21 at 17:37
  • sure, thank you for the answer – yellowcard123 May 19 '21 at 17:39

1 Answers1

0

Inner classes are compiled as stand alone classes. Java transparently passes a reference to the outer class' instance as a first parameter of the inner class' constructors. This provides access to the outer classes instance members and methods to the inner class.

Randy Casburn
  • 13,840
  • 1
  • 16
  • 31