avatarGabriel Shanahan

Summary

The provided web content offers an introduction to properties and fields in programming, with a focus on the differences between Java and Kotlin, and includes examples and resources for further learning as part of a Kotlin Primer series.

Abstract

The web content introduces the concepts of fields and properties in programming, explaining how fields represent memory blocks while properties are values presented in code. It contrasts Java's manual property creation through getters and setters with Kotlin's property-centric approach using val and var keywords. The article is part of the "Kotlin Primer" series aimed at facilitating Kotlin adoption in Java-centric organizations, with examples provided through Kotlin Playground embeds. It emphasizes that in Kotlin, properties are always declared, never fields, and must be initialized either directly or in the constructor. The content is intended as an educational resource, with acknowledgments to Etnetera a.s. for their support and a call to join their team.

Opinions

  • The author suggests that Kotlin's approach to properties is more streamlined than Java's, as Kotlin inherently defines properties rather than requiring manual creation of getters and setters.
  • The article promotes Kotlin as a language that simplifies code by automatically handling properties, which may encourage Java developers to consider Kotlin for its conciseness and ease of use.
  • By providing interactive Kotlin Playground examples, the author conveys a hands-on learning approach, encouraging readers to engage directly with Kotlin code.
  • The article expresses gratitude to Etnetera a.s. for supporting the creation of the "Kotlin Primer" series, indicating a collaborative effort in the production of educational content for the programming community.
  • The inclusion of a job advertisement for Etnetera a.s. suggests the author's endorsement of the company as a desirable workplace for those interested in Kotlin and software development.

Properties — Introduction

Definition of fields, properties, getters and setters. Difference between fields and properties. Contrasting fields and properties in Java vs. Kotlin

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

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

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

Tags: #KOTLIN FEATURE

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.

First, some definitions, so we’re all on the same page:

  • when we talk about a field, we are referring to a block of memory that has a name, type, and size that corresponds to it.
  • when we talk about a property, we are talking about a value that’s presented to (and possibly consumed from) a place in code.

A property may directly expose a field (a memory-backed property), or merely calculate a value or have a side effect (a synthetic property). It is customary for properties to be implemented as one or two functions.

  • One function, called the getter, is used to present the value.
  • The optional other function, the setter, is used to consume the value. If the setter is omitted, it’s called a read-only property.

In Java, data is stored in fields. However, Java has no syntactic mechanism to define properties. Properties in Java must be created manually, by implementing the corresponding getters/setters (and possibly a backing field).

class PublicIntField {
    public int x;
}

class PublicMemoryBackedIntProperty {
    private int x = 0;

    public int getX() {
        return x;
    }

    public void setX(final int newX) {
        x = newX;
    }
}

class PublicMemoryBackedIntReadonlyProperty {
    private int x;
    
    public PublicIntReadonlyProperty(int x) {
        this.x = x;
    }
    
    public int getX() {
        return x;
    }
}

class PublicBooleanSyntheticPropertyAndPublicIntField {
    public Int x;
    
    public boolean isXSet() {
        return x != null;
    }
}

In Kotlin, the val/var keywords always declare properties, and never fields. It is not possible to declare fields directly. This might sound confusing, but bear with us, it’ll become clearer over the next few chapters.

Here’s how you would write the previous classes in Kotlin:

When properties are not initialized directly, they must be initialized in the constructor:

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

Join me in Etnetera
Java
Kotlin
Programming
Properties
Field
Recommended from ReadMedium