Write a class in Kotlin and call it in Java

You should have written the code in Kotlin last year! I looked at the document, but when I thought that I didn't recognize it, I wrote Go last year ... I looked at Kotlin because it was a boat I got on.

Development environment is IntelliJ

"Getting Started with IntelliJ IDEA" and IntelliJ appear first in Kotlin's tutorial Getting Started, so is it popular? Download to install the community version.


Create & run Kotlin files

IntelliJ feels cool just because it's darker than when I saw it before. intellij2.png Until you create a new project, add a Kotlin file and run it, it will be crisp as "Getting Started with IntelliJ IDEA". it can.

fun main() {
    println("Hello Kotlin!")

When I installed IntelliJ, I wondered if that was the case, but the Kotlin file extension is ".kt". (Beginner)

There is a Run button to the left of the code editing the .kt file that you can click to run it. IntelliJ very convenient. kt_run.png

The file name is KotlinClassSono1.kt, but I was wondering if the Kotlin file is a class file.

How do you call this main function from Java? That's why there is a Jar file ... Even if I execute "Build Project" in IntelliJ, nothing is output, so I checked it.

Output Jar file

Open Project Structure from the File menu. project-structure.png Click the "+" button in Project Settings> Artifacts JAR> From modules with dependencies add-jar.png Click the "OK" button without specifying the Main Class createjarfrommodule.png Check "Include in project build". includeinprojectbuild.png When I run "Build Project" again ... The Jar file is output under the project folder. Thank you IntelliJ outputdir.PNG By the way, the project name is "Kotlin Component". If you look inside the Jar file jar1.PNG It was confirmed that "KotilnClassSono1Kt.class" named "File name + Kt" was included.

Try calling from Java

When I read the Jar file created in the Java project,

public class Main {
	public static void main(String[] args) {

You can see KotlinClassSono1Kt, javaexec.PNG I was able to execute Kotlin code from Java. Kt is attached to the class name, so I wondered what it is, so I went back to IntelliJ and added a package and class. Create a package with the context menu New> Package in the src folder of the package. it is normal. In the created Package, the context menu New> Kotlin File / Class !? newclass.png For the first time, I noticed the difference between File and Class.

package ari

class KotlinClassSono2 {

"Ari" is specified for Package. Let's imitate Kotlin's Class documentation and give the class the function "test". The constructor is a bonus.

package ari

class KotlinClassSono2 constructor(name: String){
    init {
        println("name = ${name}")

    constructor(name: String, age: Int) : this("$name"){
        println("age = ${age}")

    fun test() {
        println("test call OK !")

Run "Build Project" again to create a Jar file and load it in Java. I saw the "Kotlin Class Sono2" class under the "ari" package. The class name does not have "Kt". I see ~ javacode.PNG I also created an instance using the constructor. The function call is also successful. javaexec2.PNG


Kotlin is good. I don't use it, but I thought I should read the reference a little more.

Recommended Posts

Write a class in Kotlin and call it in Java
Implement Java Interface in JRuby class and call it from Java
Write a class that can be ordered in Java
StringBuffer and StringBuilder Class in Java
[Kotlin] Get Java Constructor / Method from KFunction and call it
[Java] Precautions when referencing a reference type in a parent class and instantiating it in a child class
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
Why does Java call a file a class?
A Java engineer compared Swift, Kotlin, and Java.
Creating a matrix class in Java Part 1
I wrote a Lambda function in Java and deployed it with SAM
GetInstance () from a @Singleton class in Groovy from Java
A quick review of Java learned in class
How to write Java String # getBytes in Kotlin?
How to ZIP a JAVA CSV file and manage it in a Byte array
Write ABNF in Java and pass the email address
A quick review of Java learned in class part4
Call a method with a Kotlin callback block from Java
Browse class objects in Kotlin (instead of Java class name.class)
How to convert A to a and a to A using AND and OR in Java
A quick review of Java learned in class part3
A quick review of Java learned in class part2
Use of Abstract Class and Interface properly in Java
Call a program written in Swift from Processing (Java)
[Java] [Kotlin] Generically call valueOf and values of Enum
Write class inheritance in Ruby
Write flyway callbacks in Java
Java class definition and instantiation
Find a subset in Java
[Java] Get KClass in Java [Kotlin]
[Java] Make it a constant
Write Java8-like code in Java8
Assign a Java8 lambda expression to a variable and reuse it
Implement Thread in Java and try using anonymous class, lambda
Memorandum No.4 "Get a character string and decorate it" [Java]
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
How to call and use API in Java (Spring Boot)
Java Language Feature and How it Produced a Subtle Bug
Define abstract methods in Java enum and write each behavior
How to develop and register a Sota app in Java
[MQTT / Java] Implemented a class that does MQTT Pub / Sub in Java
[Java] A class is an OS, and an instance is a virtual computer.
Sort List in descending order in Java and generate a new List non-destructively
Call Kotlin's sealed class from Java
Differences between "beginner" Java and Kotlin
3 Implement a simple interpreter in Java
I created a PDF in Java.
Encoding and Decoding example in Java
Kotlin Class to send to Java developers
Handle business logic for a set of Entity in Java class
Mechanism and characteristics of Collection implementation class often used in Java
Compare Hello, world! In Spring Boot with Java, Kotlin and Groovy
(Ruby on Rails6) Creating a database and displaying it in a view
Write keys and values in Ruby
A simple sample callback in Java
I wrote a route search program in TDD and refactored it
Creating a matrix class in Java Part 2-About matrices (linear algebra)-
Understanding equals and hashCode in Java
Make a SOAP call in C #