Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Defining Your First Kotlin Class | Introduction to Classes and Objects
Kotlin Classes and Objects

bookDefining Your First Kotlin Class

Swipe um das Menü anzuzeigen

Class Declaration

To define a class in Kotlin, you use the class keyword followed by the class name. The class name should always start with an uppercase letter and use camel case for multiple words. The simplest form of a class includes the keyword, the name, and a pair of curly braces to enclose the class body. The class body is where you will later add properties and methods, but it can be left empty when you first create the class.

Naming Conventions

Kotlin follows standard naming conventions for classes:

  • Class names should use PascalCase (each word starts with a capital letter);
  • Names must be descriptive and clearly indicate the purpose of the class;
  • Avoid using underscores or abbreviations in class names.
Person.kt

Person.kt

copy

The Person class above illustrates the basic structure of a Kotlin class. The class keyword signals that you are defining a new class. Person is the class name, following the naming conventions of starting with an uppercase letter. The curly braces {} define the body of the class, which is currently empty. Later, you can add properties or methods inside these braces to give your class functionality. Even if the class body is empty, the braces are required if you want to include members in the future.

question mark

Which keyword is used to define a class in Kotlin?

Wählen Sie die richtige Antwort aus

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 2

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Abschnitt 1. Kapitel 2
some-alt