How to create a static extension method in Kotlin?
Hi everyone! I recently started learning Kotlin after Java and ran into a problem.\n\nIn Java, I often used static methods in utility classes, for example:\n\n```java\npublic class StringUtils {\n public static boolean isPalindrome(String s) {\n return s.contentEquals(new StringBuilder(s).reverse());\n }\n}\n```\n\nIn Kotlin, I know you can write extension functions to add methods to existing classes. For example:\n\n```kotlin\nfun String.isPalindrome(): Boolean {\n return this == reversed()\n}\n```\n\nNow I can call `"hello".isPalindrome()`. But what if I want to make such a method static? That is, so that it can be called as `String.isPalindrome("hello")` — directly through the class name, like in Java?\n\nI tried writing a `companion object` with functions
No answers yet.
Log in to leave an answer
Blogs
How to Write a REST API in Swift in 30 Minutes: A Beginner's Guide
Why Java Executes Code in Comments via Unicode: Analysis
How to Write a REST API in Kotlin in 30 Minutes: A Step-by-Step Guide
Multiline Strings in Rust: Syntax, Examples, and Secrets