Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Kotlin for Enterprise Applications using Java EE
  • Table Of Contents Toc
Kotlin for Enterprise Applications using Java EE

Kotlin for Enterprise Applications using Java EE

By : Rao K
close
close
Kotlin for Enterprise Applications using Java EE

Kotlin for Enterprise Applications using Java EE

By: Rao K

Overview of this book

Kotlin was developed with a view to solving programmers’ difficulties and operational challenges. This book guides you in making Kotlin and Java EE work in unison to build enterprise-grade applications. Together, they can be used to create services of any size with just a few lines of code and let you focus on the business logic. Kotlin for Enterprise Applications using Java EE begins with a brief tour of Kotlin and helps you understand what makes it a popular and reasonable choice of programming language for application development, followed by its incorporation in the Java EE platform. We will then learn how to build applications using the Java Persistence API (JPA) and Enterprise JavaBeans (EJB), as well as develop RESTful web services and MicroServices. As we work our way through the chapters, we’ll use various performance improvement and monitoring tools for your application and see how they optimize real-world applications. At each step along the way, we will see how easy it is to develop enterprise applications in Kotlin. By the end of this book, we will have learned design patterns and how to implement them using Kotlin.
Table of Contents (13 chapters)
close
close

Classes in Kotlin

Classes in Kotlin are created using the class keyword, which is depicted as follows:

class User {

}

The structure of the class is as follows:

Class className {
//properties and constructor
//member functions
}

Let's write a class for User.

Consider the following code for 14_Class.kts:

class User {
var name = "George"
}

val user = User()
println(user.name)

The output is as follows:

This class has a property called name. Classes are instantiated with just the class name, followed by parentheses. Note that there is no new keyword in Kotlin to create an object, unlike in Java.

Classes in Kotlin are final by default. We cannot extend from a class directly. To inherit from the classes, we have to open the classes explicitly. Classes need to have an open keyword in their declaration, as shown in the following code:

open class Person {

}

Alternatively, we can use all-open compiler plugins to make all the classes extendable or accessible to frameworks such as JPA and Spring. We will discuss compiler plugins further in the next chapter.

Constructors

Constructors are used to initialize class properties. As in Java, a constructor is a special member function that is invoked when an object is instantiated. However, they work slightly different in Kotlin.

In Kotlin, there are two constructors:

  • Primary constructor: This is a concise way to initialize the properties of a class
  • Secondary constructor: This is where additional initialization logic goes

The primary constructor is part of the class header. Here's an example:

class User() {

}

The block of code surrounded by parentheses is the primary constructor. Consider the following code for 14a_PrimaryConstructor.kts:

class User(var firstName: String, var lastName: String) {

}

val user = User("Norman", "Lewis")
println("First Name= ${user.firstName}")
println("Last Name= ${user.lastName}")

The output is as follows:

The constructor goes here as part of the class header. The constructor declares two properties—firstName and lastName. Let's look at another example for 14b_PrimaryConstructor.kts:

class User(var firstName: String, val id: String) {

}
val user = User("Norman", "myId")
println("First Name = ${user.firstName}")
println("User Id = ${user.id}")

The output of the preceding code is as follows:

For the primary constructor, properties have to be declared using var or val. Otherwise, the code fails to compile.

The secondary constructor is created using the constructor keyword. The class can declare a secondary constructor to initialize its properties. This is shown in the following code:

class AuditData {
constructor(message: String) {
//init logic
}
constructor(message: String, locale: String) {
// init logic
}
}

In the preceding code snippet, we wrote two constructors. One had a message as an argument and one had two arguments—message and locale. Consider the following code for 14c_SecondaryConstructor.kts:

var audit = AuditData("record added")
println("Message ${audit.message}")

audit = AuditData("record updated", "en-US")
println("Message: ${audit.message}")
println("Locale: ${audit.locale}")

When we call AuditData with only a message, the constructor with one argument will be invoked. Similarly, when we pass two arguments, a message and a locale, the constructor with two arguments will be invoked.

The output is as follows:

Static functions

Static functions are functions that can be invoked without creating multiple instances of a class. Static functions avoid code duplication and can be reused.

Let's take a look at how to write a static function in Kotlin.

Consider the following code for 15a_StaticMethods.kts:

object MyUtil {
fun foo(){
println("Static function foo() is invoked")
}
}

MyUtil.foo()

Note that we declared an object MyUtil and defined a function foo(). This is known as object declaration.

We invoked the function foo() directly using the object MyUtil.

The output of the preceding code is as follows:

There are different ways to write static functions in Kotlin. We can define a companion object inside a class and define a static function in it. Consider the following code for 15b_StaticMethods.kts:

class Person {
companion object {
fun foo(){
println("Static function foo() is invoked")
}
}
}
Person.foo()

The output is as follows:

We can also give a name to the companion object. Consider the following code for 15c_StaticMethods.kts:

class Person {
companion object Util {
fun foo(){
println("Static function foo() is invoked")
}
}
}
Person.Util.foo()

The output is as follows:

We can invoke the static method shown in the preceding example with companion as Person.Companion.foo(). The static method foo can be invoked using either the class name or a named companion object prefixed with the class name, such as Person.Util.foo().

In this section, we have covered the constructs that Kotlin provides. We will be using these constructs in the next few chapters.

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Kotlin for Enterprise Applications using Java EE
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon