Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – How to access primary constructor parameters inside secondary constructor kotlin

Posted on November 11, 2022 By

Solution 1 :

You have to call your secondary constructor, which expects a trailing name parameter:

    val person1 = Person("Joe", 25, "name") //prints Age = 25
    val person2 = Person("Joe", 25) //prints nothing

In your example, the primary constructor gets chosen as your argument list maps its parameters.

Solution 2 :

in your code val person1 = Person("Joe", 25) your are calling a constructor wich have two parameters and thats the first constructor not the second.
call the second constructor like that val person1 = Person("Joe", 25,"name").
i hope that was clear.

Problem :

I am learning kotlin and I was reading about constructors: primary and secondary.

Here is my question that how should I access primary constructor parameters inside a secondary constructor. I am unable to access but I am not sure why? Why I am not able to access it directly?
If anyone knows please help me understand this concept in a better way. Why I am not able to access it?

I have created one demo class with two constructors here is my code:

fun main(args: Array<String>) {

    val person1 = Person("Joe", 25)
    println("First Name = ${person1.firstName}") // working, printing first name 

}

class Person(val firstName: String, var age: Int) {

    constructor(sectionName: String, id: Int, name: String) : this(sectionName,id)  {
        println("Age = ${age}") // not working, no output
    }

}

Or Am I doing anything wrong?

PS: I know I can write init block and assign the parameters to the class variable and it is working as expected.

Comments

Comment posted by Ryan M

You’re not calling the secondary constructor.

Comment posted by Harsh Shah

What a silly mistake thank you for pointing out. 🙂

READ  [FIXED] java - Android Stretching Items in TableLayout XML
Powered by Inline Related Posts
Android Tags:android, constructor, kotlin, primary-constructor

Post navigation

Previous Post: [FIXED] android – How to pass data by Back Button, Kotlin
Next Post: [FIXED] Project with external module issue in android studio 3.6

Related Posts

[FIXED] C++ library causing crash in native Android Android
[FIXED] android – How to block retrieving ArrayList items during of inserting data into it Android
[FIXED] java – Hide the keyboard in autoComplete Android
[FIXED] Which file(s) do I use for an Android app built with Android Studio Android
[FIXED] How to add onClickListener on a RecyclerView in Android? Android
[FIXED] How to decrease frame rate of Android CameraX ImageAnalysis? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What color are dead flea eggs?
  • What is Indiana vine?
  • What’s the downside of a Chromebook?
  • Is phosphide the same as phosphorus?
  • Why do you need an S bend?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme