Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Introduction to Kotlin  ›  Capstone Project

The main Function

main Function 12 minEvery Kotlin program starts at a top-level function named main, which can take no parameters or an array of command-line arguments.

The main Function

You press Run and the café app springs to life. But which line runs first? In a project with hundreds of functions, the JVM needs one agreed-upon starting point, and in Kotlin that starting point is always called main.

The rule

A program starts at top-level main, no class required.

The smallest complete Kotlin program:

fun main() {
    println("Pebble Café is open")
}

Reading it word by word:

  • fun declares a function.
  • main is the special name the runtime looks for.
  • () means it takes no parameters.
  • The body inside { } runs from top to bottom; when it ends, the program ends.

It sits at the top level of a file, outside any class. Java needs public static void main(String[] args) inside a class; Kotlin needs none of that.

If you want words typed after the program name on the command line, use the second form:

fun main(args: Array<String>) {
    println("Pebble Café is open")
    if (args.isNotEmpty()) {
        println("Serving table ${args[0]}")
    }
}

Running java -jar cafe.jar 7 makes args hold one string, "7", and the program prints "Serving table 7". Note that arguments always arrive as strings; turning "7" into the number 7 is your job.

main returns nothing useful. Its return type is Unit, Kotlin's word for "no meaningful value", and you do not need to write it.

The shortcut

To predict a program's behaviour, find main and read only it, jumping into other functions only when main calls them. Everything outside main just defines things; nothing happens until main asks for it.

What does this file print?Step 1 of 4
  1. Scan the file

    It declares fun greet() { println("Hello") } and fun main() { println("Open"); greet() }.

A classic mistake is spelling it Main or placing it inside a class without extra annotations. The runtime then cannot find the entry point and reports that no main method exists.

Drills

Answer fast — trust the rule.

quick drill

Where does a Kotlin JVM program start running?

quick drill

Which is a valid Kotlin entry point?

quick drill

The program is started with java -jar cafe.jar 4 latte. What is args[1]?

quick drill

A file defines fun open() { println("A") } and then fun main() { println("B") }. What is printed?

quick drill

What is the return type of fun main() { }?

Remember

  • Every program starts at top-level main; no class needed.
  • fun main() or fun main(args: Array<String>); arguments are strings.
  • To predict output, read main and follow only the calls it makes.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhere the café program starts

Write a main function for the café app that prints a welcome line and, if a table number is passed on the command line, prints it too. Explain what main is, why it needs no class around it, and what the two forms of main are.