44

As i have one User class having 2 parameters : first_name, last_name. So my kotlin class with be :

data class User(val first_name:String, val last_name:String) 

Now i want a constructor which will accept only first_name, or you can say just one parameter. How can i define it with Kotlin?

I know we can pass default value and in that way we can ignore second parameter, but how can we write multiple constructor?

3
  • for this case .. you don't want to create another constructor.. because in Kotlin , we can use parameters as optional Commented May 25, 2017 at 8:51
  • 2
    kotlinlang.org/docs/reference/classes.html Commented May 25, 2017 at 8:52
  • 1
    Why do you want a second constructor instead of a default value for last_name? And btw, in java and kotlin you should use CamelCase instead of SnakeCase Commented May 25, 2017 at 8:55

5 Answers 5

68

You can define extra constructors in the class body

data class User(val firstName: String, val lastName: String) { constructor(firstName: String) : this(firstName, "") } 

These 'secondary constructors' have to call through to the primary constructor or a different secondary constructor. See the Official documentation on constructors.

So, in effect this is the same as just a primary constructor with default argument, which would be the idiomatic way to go.

data class User(val firstName: String, val lastName: String = "") 
Sign up to request clarification or add additional context in comments.

Comments

6

I hope this will help you

class Person(val name: String, val age: Int = 0) { override fun toString(): String { return name + " is " + age + " years Old" } } fun main(args: Array<String>) { var person = Person(name = "vignesh") var personNew = Person("vignesh", 23) println(person.toString()) println(personNew.toString()) } 

Output

vignesh is 0 years Old

vignesh is 23 years Old

Comments

2

If you are using data class, then you won't require another constructor. Just pass default value to your last_name parameter. If you are using a normal class then you can have secondary constructor Lets say you have class A

class A(val param:String,val param2:String){ constructor(val param:String):this(param,"") } 

If you wish manipulate these values you can use init{} block where you can play around your constructor values.

I hope this will help.

Comments

2

This sample of code works fine for me, you can customize them to your need.

 data class Booking( var user: String, var bike: String ){ constructor( user: String, bike: String, taken_at: String, returned_at: String ) : this (user, bike) } 

2 Comments

if we do this: val x = Booking("foo", "bmx", "10am", "1030pm"), how can we print out the values for taken_at and returned_at? seems like we can't do: println(x.taken_at) it doesn't work.
If you need to display them too, you can add their variable so that they can be accessed.
1

A class in Kotlin can have a primary constructor and one or more secondary constructors. The primary constructor is part of the class header: it goes after the class name (and optional type parameters).

class Person constructor(firstName: String) { } 

If the primary constructor does not have any annotations or visibility modifiers, the constructor keyword can be omitted:

class Person(firstName: String) { } 

Note that parameters of the primary constructor can be used in the initializer blocks. They can also be used in property initializers declared in the class body:

class Customer(name: String) { val customerKey = name.toUpperCase() } 

You can also follow this link as per your need : Kotlin

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.