A
A
Aleksey Pleshkov2016-10-21 12:46:50
Java
Aleksey Pleshkov, 2016-10-21 12:46:50

How to correctly specify variables in Kotlin?

Good day. I use Kotlin and Spring. Tell me how to work with variables (in the structure below) in order to avoid constant checking for NULL and writing "!!"?
There are such variables in the class

@Autowired
    var newsModules: NewsModules

    @Autowired
    var usersModules: UsersModules

    @Autowired
    var regionsModules: RegionsModules

    @Autowired
    var productModule: ProductsModules

    @Autowired
    var requestsModules: RequestsModules

If you do not specify " init "
init {
        newsModules = NewsModules()
        usersModules = UsersModules()
        regionsModules = RegionsModules()
        productModule = ProductsModules()
        requestsModules = RequestsModules()
    }

Then you need to initialize the variables in this way
@Autowired
    var newsModules: NewsModules? = null
...

And this entails a constant check for " null " and prescribing " !! ". And, as I understand it, the use of " init() " is not quite correct here, since " @Autowired " is used.
Please tell me how to make it better. Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bendikv, 2016-10-21
@AlekseyPleshkov

lateinit
https://kotlinlang.org/docs/reference/properties.html
Also worth a look:
https://spring.io/blog/2016/02/15/developing-sprin...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question