Android Kotlin - Classes And Objects - Part Four

This article is a part of a series of articles on Kotlin. Before continuing with this, please have a look at my previous articles.

  1. Android Kotlin - Hello World - Part One
  2. Android Kotlin-Variables, Data Types and If-Else, When Statements - Part Two
  3. Android Kotlin - Strings and Ranges - Part Three

Step 1

Let’s add a class called Employee. Right-click on package and click New >> Kotlin File/Class. Name it Employee and click OK.

Kotlin

Kotlin

Step 2

Create two variables - name and age - in Employee class.

In Kotlin, we have to assign some values either empty or null; otherwise, Kotlin compiler gives an error.

So, it looks like the following.

  1. class Employee {  
  2.     var name: String = ""    //defining empty string.  
  3.     var age: Int = 0  
  4. }  

Step 3

In MainActivity.kt, let's create an object of Employee class.

  1. var employee = Employee();  

Step 4

Let’s add two TextViews in activity_main.xml.

  1. <?xml version="1.0" encoding="utf-8"?> ,
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="com.example.kotlin_helloworld.MainActivity">  
  8.   
  9.     <LinearLayout  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:orientation="vertical">  
  13.   
  14.         <TextView  
  15.             android:id="@+id/txtEmpName"  
  16.             android:layout_width="wrap_content"  
  17.             android:layout_height="wrap_content"  
  18.             android:text=""  
  19.             android:textSize="20dp"  
  20.             app:layout_constraintBottom_toBottomOf="parent"  
  21.             app:layout_constraintLeft_toLeftOf="parent"  
  22.             app:layout_constraintRight_toRightOf="parent"  
  23.             app:layout_constraintTop_toTopOf="parent" />  
  24.   
  25.         <TextView  
  26.             android:id="@+id/txtEmpAge"  
  27.             android:layout_width="wrap_content"  
  28.             android:layout_height="wrap_content"  
  29.             android:text=""  
  30.             android:textSize="20dp"  
  31.             app:layout_constraintBottom_toBottomOf="parent"  
  32.             app:layout_constraintLeft_toLeftOf="parent"  
  33.             app:layout_constraintRight_toRightOf="parent"  
  34.             app:layout_constraintTop_toTopOf="parent" />  
  35.     </LinearLayout>  
  36. </android.support.constraint.ConstraintLayout>  

Step 5

In MainActivity.kt, we can set values to the TextViews,

  1. var employee = Employee();txtEmpName.setText("Employee Name = ${employee.name}")  
  2. txtEmpAge.setText("Employee Age = ${employee.age}")  

Step 6

Run the application and you can see the following output. Since their default values are empty, they are displayed as below.

Kotlin

 

Step 7

Let’s add a function to set the values for the Employee class.

In Employee.kt, add the following function,

  1. fun setEmployee(empName: String, empAge: Int) {  
  2.     name = empName  
  3.     age = empAge  
  4. }  

So, from MainActivity.kt, we can call this function.

MainActivity.kt will look like,

  1. override fun onCreate(savedInstanceState: Bundle ? ) {  
  2.     super.onCreate(savedInstanceState)  
  3.     setContentView(R.layout.activity_main)  
  4.     var employee = Employee();  
  5.     employee.setEmployee("Kishor", 23) //sets the values to the variables  
  6.     txtEmpName.setText("Employee Name = ${employee.name}")  
  7.     txtEmpAge.setText("Employee Age = ${employee.age}")  
  8. }  

Step 8

Run the application and you can see the following output.

Kotlin

 

Step 9

One unique feature in Kotlin is that the parameters you pass in function are not necessary to be in order. While calling the setEmployee function from MainActivity.kt, we can pass parameters like below.

  1. employee.setEmployee( empAge = 23, empName = "Kishor")  

This will give the same output.

Kotlin

 

Step 10

If you want to assign some null values to the variables, then you can do as below.

  1. var name: String? = null  
  2. var age: Int? = null  

When you run the application, again, this will give the same result.

Get the project from GitHub.

Thanks, and happy coding.