Press enter to see results or esc to cancel.

Lesson 26: Calling methods from another method of the same class – kotlin

Share this

So far all the problems raised have called the methods from where we define an object of that class, for example:

    Val persona1 = Person ("Juan", 12)
    Print ()
    Person1.MajorAge ()

We use the syntax:

[Name of the object] [method name]

That is, we precede the name of the method with the name of the object and the operator point.

Now what happens if we want to call within the class another method that belongs to the same class, the syntax is as follows:

[Method name]

O in long form:

This [method name]

It is important to keep in mind that this can only be done when we are within the same class.

Problem 1

Putting a class Operations that in one method requests the loading of 2 integers and then call two other methods that calculate their sum and product.

Project117 – Principal.kt

class operations {
    var value1: Int = 0
    var value2: Int = 0

    fun load () {
        print ("Enter first value:")
        value1 = readLine () !!. toInt ()
        print ("Enter second value:")
        value2 = readLine () !!. toInt ()

    fun sumar () {
        val sum = value1 + value2
        println ("The sum of $value1 and $value2 is $sum")

    funprize () {
        val subtraction = value1 - value2
        println ("The subtraction of $value1 and $value2 is $subtraction")

fun main (parameter: Array <String>) {
    val operations1 = Operations ()
    operations1.load ()

Our method load in addition to loading the two integers in the properties proceeds to call the methods that calculate the addition and subtraction of the two values ​​entered.

The call of the methods of the same class is done indicating the name of the method.

From where we define an object of the class Operations we call its methods preceding the name of the object:

    Val operations1 = Operations ()
    Operations1.load ()

Proposed problem

  • Declare a class called Children. Define within it an arrangement to store the ages of 5 people.
    Define a load method where you enter the age arrangement and call two other methods that print the oldest and the average age.

class children {
    val ages = IntArray (5)

    fun load () {
        for (i in ages.indices) {
            print ("Enter age:")
            ages [i] = readLine () !!. toInt ()

     fun majorAge () {
        var major = ages [0]
        for (i in ages.indices)
            if (ages [i]> major)
                major = ages [i]
        println ("The oldest child is $major")

    fun average () {
        var sum = 0
        for (i in ages.indices)
            sum += ages [i]
        val average = sum / ages.size
        println ("The average age of children is $average")

fun main (Parameters: Array <String>) {
    val o = children ()
    o.load ()

How useful was this Lesson?

Click on a star to rate it!

Average rating / 5. Vote count:

Be the first to rate this post.!

As you found this post useful...

Follow us on social media!

We are sorry that this lesson was not useful for you!

Let us improve this lesson!

Tell us how we can improve this lesson?

Share this