There is no new
keyword in Kotlin: constructors are called directly.
Kotlin
var myObject = MyClass()
Here’s where things get funky.
A class’s primary constructor is defined inline with the class declaration.
C#
public class MyClass
{
public MyClass(string s)
{
//...
}
}
Kotlin
class MyClass constructor(s: String) {
}
The constructor here (like everything in Kotlin) is implicitly public
, so if that’s what we want we can omit the constructor
keyword.
class MyClass(s: String) {
}
Or we can make it private
/protected
/internal
.
class MyClass private constructor(s: String) {
}
If we need to access s
we can do it in an init
block.
class MyClass(s: String) {
private val someProperty: String
init {
someProperty = s
}
}
But a more common pattern is to initialise properties directly in the constructor by adding the val
or var
keyword.
class MyClass(val someProperty: String) {
}
var myObject = MyClass("hello world")
println(myObject.someProperty) // "hello world"
This looks 😵💫 at first sight but it makes for nice small classes when you compare with C#.
C#
public class Service
{
private readonly IRepository _repository;
private readonly ILogger _logger;
public string Name { get; set; }
public Service(IRepository repository, ILogger logger, string name)
{
_repository = repository;
_logger = logger;
Name = name;
}
}
Kotlin
class Service(
private val repository: Repository,
private val logger: Logger,
var name: String
) {
}