Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between a class and object in Kotlin

I'm new to Kotlin and have recently converted a simple file from java to Kotlin. I am wondering why the Android converter changed my java class to a Kotlin object.

Java:

public class MyClass {
    static public int GenerateChecksumCrc16(byte bytes[]) {

        int crc = 0xFFFF;
        int temp;
        int crc_byte;

        for (byte aByte : bytes) {

            crc_byte = aByte;

            for (int bit_index = 0; bit_index < 8; bit_index++) {

                temp = ((crc >> 15)) ^ ((crc_byte >> 7));

                crc <<= 1;
                crc &= 0xFFFF;

                if (temp > 0) {
                    crc ^= 0x1021;
                    crc &= 0xFFFF;
                }

                crc_byte <<= 1;
                crc_byte &= 0xFF;

            }
        }

        return crc;
    }
}

Converted Kotlin:

object MyClass {
    fun GenerateChecksumCrc16(bytes: ByteArray): Int {

        var crc = 0xFFFF
        var temp: Int
        var crc_byte: Int

        for (aByte in bytes) {

            crc_byte = aByte.toInt()

            for (bit_index in 0..7) {

                temp = crc shr 15 xor (crc_byte shr 7)

                crc = crc shl 1
                crc = crc and 0xFFFF

                if (temp > 0) {
                    crc = crc xor 0x1021
                    crc = crc and 0xFFFF
                }

                crc_byte = crc_byte shl 1
                crc_byte = crc_byte and 0xFF

            }
        }

        return crc
    }
}

Why wasn't it:

class MyClass {
    ... etc ...
}

Any help would be greatly appreciated thanks.

like image 734
Crunchy234 Avatar asked Sep 27 '22 21:09

Crunchy234


People also ask

What is the difference between class & object?

Object is an instance of a class. Class is a blueprint or template from which objects are created. Object is a real world entity such as pen, laptop, mobile, bed, keyboard, mouse, chair etc. Class is a group of similar objects.

What is object class in Android Kotlin?

Classes and objects in Kotlin work the same way as in most object-oriented languages: a class is a blueprint, and an object is an instance of a class.

What is the use of object in Kotlin?

Kotlin has an object keyword, which combines both declaring a class and creating an instance of it in one action.


1 Answers

Kotlin's documentation on this is pretty good, so feel free to read that.

The chosen answer for this question has some poor phraseology in its explanation, and could easily mislead people. For instance, an object is not "a static class per se", but rather it is a static instance of a class that there is only one of, otherwise known as a singleton.

Perhaps the best way to show the difference is to look at the decompiled Kotlin code in Java form.

Kotlin object and class:

object ExampleObject {
  fun example() {
  }
}

class ExampleClass {
  fun example() {
  }
}

In order to use the ExampleClass, you need to create an instance of it: ExampleClass().example(), but with an object, Kotlin creates a single instance of it for you, and you don't ever call it's constructor, instead you just access it's static instance by using the name: ExampleObject.example().

Equivalent Java code Kotlin would generate:

Kotlin compiles to Java byte code, but if we reverse compile the above compiled Kotlin code to Java code this is what we get:

public final class ExampleObject {
   public static final ExampleObject INSTANCE = new ExampleObject();

   private ExampleObject() { }

   public final void example() {
   }
}

public final class ExampleClass {
   public final void example() {
   }
}

You would use the object in Kotlin the following way:

ExampleObject.example()

Which would compile down to the equivalent Java byte code for:

ExampleObject.INSTANCE.example()

Why does Kotlin introduce objects?

The primary use case of object in Kotlin is because Kotlin tries to do away with static, and primitives, leaving us with a purely object oriented language. Kotlin still uses static and primitives underneath the hood, but it discourages devs to use those concepts any more. Instead, now Kotlin replaces static with singleton object instances. Where you would previously use static field in Java, in Kotlin you will now create an object, and put that field in the object.

Interoperability with Java:

Because Kotlin is 100% interoperable with Java, sometimes you will want to expose certain APIs or fields in a way that is nicer for Java to read. To do this, you can use the @JvmStatic annotation. By annotating a field or a function in an object with @JvmStatic, it will compile down to static fields which Java can use easier.

Companion Objects:

One last thing that's worth mentioning is companion objects. In Java, you typically have classes that have some static content, but also some non-static / instance content. Kotlin allows you to do something similar with companion objects, which are objects tied to a class, meaning a class can access it's companion object's private functions and properties:

class ExampleClass {
  companion object {
    // Things that would be static in Java would go here in Kotlin
    private const val str = "asdf"
  }

  fun example() {
    // I can access private variables in my companion object
    println(str)
  }
}
like image 252
spierce7 Avatar answered Oct 10 '22 08:10

spierce7