avatarGabriel Shanahan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1187

Abstract

ockquote><blockquote id="757e"><p>It is recommended to read the <a href="https://readmedium.com/introduction-2cc7eba631e2">Introduction</a> before moving on. Check out the <a href="https://readmedium.com/table-of-contents-c52573cfa291">Table of Contents</a> for all articles.</p></blockquote><p id="d7c0">If the value of a read-only property is known at compile time, you can mark it as a compile time constant using the <code>const</code> modifier.</p><p id="b6f0">Such a property needs to fulfill the following requirements:</p><ul><li>It must be a top-level property, or a member of an <a href="https://readmedium.com/object-declarations-49f5786ff86b">object declaration</a> or a <a href="https://readmedium.com/companion-objects-a55209c0074a">companion object</a> (more on both of those later)</li><li>It must be initialized with a value of type <code>String</code> or a primitive type</li><li>It cannot have a custom getter (and, since it’s a constant, it naturally can’t have a setter either)</li></ul><p id="85fc">Such properties can be used in annotations:</p> <figure id="1a3f"> <div> <div> <img class="ratio" src="http://placeh

Options

old.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2Ff8DgLEfG_&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2Ff8DgLEfG_&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="cdac">Go back to <a href="https://readmedium.com/accessors-continued-a1a57a2c4e4e">Accessors, continued</a>, jump to the <a href="https://readmedium.com/table-of-contents-c52573cfa291">Table of Contents</a>, or continue to <a href="https://readmedium.com/lateinit-294c6177bfda">Lateinit</a>.</p><figure id="8ecd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*biBSB579iezsNvEQ_NMLBg.png"><figcaption><a href="https://www.etnetera.cz/prace-u-nas?utm_source=medium&amp;utm_medium=GabrielShanahan&amp;utm_campaign=KotlinPrimer&amp;utm_content=join-our-team&amp;utm_term=KotlinPrimer#pozice">Join me in Etnetera</a></figcaption></figure></article></body>

Compile Time Constants

A short note on compile time constants and their usage in annotations

— — — — — — — — — — — — — — —

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

— — — — — — — — — — — — — — —

Tags: #FYI

This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.

It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.

If the value of a read-only property is known at compile time, you can mark it as a compile time constant using the const modifier.

Such a property needs to fulfill the following requirements:

  • It must be a top-level property, or a member of an object declaration or a companion object (more on both of those later)
  • It must be initialized with a value of type String or a primitive type
  • It cannot have a custom getter (and, since it’s a constant, it naturally can’t have a setter either)

Such properties can be used in annotations:

Go back to Accessors, continued, jump to the Table of Contents, or continue to Lateinit.

Join me in Etnetera
Kotlin
Java
Programming
Compile Time Constants
Annotations
Recommended from ReadMedium