https://kotlinlang.org logo
Title
e

Esa

09/17/2019, 12:55 PM
Hi, I have a spring boot project in which I’ve a Service (just a
class xyzService.kt
). In this service, i have several private values. Now, for the usecase I’m trying to solve in this service, I wanted to have an
abstract inner class Action
which has 1 more layer of inner classes, like
inner class Action.Cancel
,
Action.Start
etc (actual usecases are a bit more complex but I guess this translates okay). Each of these inner classes should also have access to the values in the top level service class. Then from the service class (but outside the Action class) I wish to be able to do
Action.Cancel().execute()
(execute is an overridden function from the superclass). However, I seem to run into some issues with these visibility modifiers. When attempting to interact with the values in the top level service class, I get the following:
Constructor of inner class Start can be called only with receiver of containing class
. Am I using the wrong hierarchy here? What’d be more appropriate? Does this seem like a wrong way to do things?
t

thanksforallthefish

09/17/2019, 1:07 PM
you have the same issue in java if you have something like
public class Outer {
  public class Inner {
  }
}
you cannot call
new Outer.Inner()
. if you want to use this syntax, inner must be
static
, otherwise you need to do
new Outer().Inner()
inner
classes need to hold a pointer to the outer ones, so basically you need to remove the
inner
keyword in kotlin
m

mister11

09/17/2019, 1:34 PM
A better approach would be to extract that class hierarchy to another file. It would fix your problem and improve code readability and organization. Have in mind that naming would also change probably if that
Action
is specific for that particular service.
e

Esa

09/18/2019, 5:29 AM
That would be difficult. The service class holds a few other services as values, and the Action class and subclasses hold feature-level logic from each of these services. A bit tightly coupled.
@thanksforallthefish if I remove the inner keyword, the Action class is unable to reach the private values of the service. Do you know of another way I can be able to do this while still reaching the private values? The only way I found was having the action class be open inner class, but that does not seem right.