
- Android Basics
- Android - Home
- Android - Overview
- Android - Environment Setup
- Android - Architecture
- Android - Application Components
- Android - Hello World Example
- Android - Resources
- Android - Activities
- Android - Services
- Android - Broadcast Receivers
- Android - Content Providers
- Android - Fragments
- Android - Intents/Filters
- Android - User Interface
- Android - UI Layouts
- Android - UI Controls
- Android - Event Handling
- Android - Styles and Themes
- Android - Custom Components
- Android Advanced Concepts
- Android - Drag and Drop
- Android - Notifications
- Location Based Services
- Android - Sending Email
- Android - Sending SMS
- Android - Phone Calls
- Publishing Android Application
- Android Useful Examples
- Android - Alert Dialoges
- Android - Animations
- Android - Audio Capture
- Android - AudioManager
- Android - Auto Complete
- Android - Best Practices
- Android - Bluetooth
- Android - Camera
- Android - Clipboard
- Android - Custom Fonts
- Android - Data Backup
- Android - Developer Tools
- Android - Emulator
- Android - Facebook Integration
- Android - Gestures
- Android - Google Maps
- Android - Image Effects
- Android - ImageSwitcher
- Android - Internal Storage
- Android - JetPlayer
- Android - JSON Parser
- Android - Linkedin Integration
- Android - Loading Spinner
- Android - Localization
- Android - Login Screen
- Android - MediaPlayer
- Android - Multitouch
- Android - Navigation
- Android - Network Connection
- Android - NFC Guide
- Android - PHP/MySQL
- Android - Progress Circle
- Android - ProgressBar
- Android - Push Notification
- Android - RenderScript
- Android - RSS Reader
- Android - Screen Cast
- Android - SDK Manager
- Android - Sensors
- Android - Session Management
- Android - Shared Preferences
- Android - SIP Protocol
- Android - Spelling Checker
- Android - SQLite Database
- Android - Support Library
- Android - Testing
- Android - Text to Speech
- Android - TextureView
- Android - Twitter Integration
- Android - UI Design
- Android - UI Patterns
- Android - UI Testing
- Android - WebView Layout
- Android - Wi-Fi
- Android - Widgets
- Android - XML Parsers
- Android Useful Resources
- Android - Questions and Answers
- Android - Useful Resources
- Android - Discussion
What is the equivalent of Java static methods in Kotlin?
In Java, "static" keyword is used for efficient memory management. Once a variable or method is declared as static, then the JVM will allocate memory for these variable only once. Usually static variables are used to declare common properties of a class, for example, "Name of the institution". In the following example, we will see how to use the static keyword.
Example of Static in using Java
In order to demonstrate how static works in Java, we will access our online Java compiler and we will create a Test class. Inside Test, we will try to create a static variable along with a static method and we will access both of them without creating any of its class objects.
Example
public class Test { static int myStaticVariable = 10; // static method static void staticMethod() { System.out.println("www.tutorialspoint.com"); } public static void main(String[] args) { // accessing static method and variable without creating // any object of class Test staticMethod(); System.out.println("Accessing Static variable-"+myStaticVariable); } }
Output
Once you execute the code, it will generate the following output −
$javac Test.java $java -Xmx128M -Xms16M Test www.tutorialspoint.com Accessing Static variable-10
Equivalent of Java Static Methods in Kotlin
In Kotlin, we don't have a static keyword. In this article, we will learn how we can achieve the same memory management using a different keyword available in the Kotlin library. The objective is to implement a different Kotlin library function to opt a condition where memory will be created only once and its value cannot be modified from other section of the application.
There are two ways to use static in Kotlin −
Using companion object
Using object class and @JvmStatic annotation
Let's understand each of these ways in detail.
Using Companion Object
Adding a companion in the object will help developers to achieve static functionality in Kotlin. It stores the object in the same file where the class is stored, hence it can access all the private methods and variables inside the class. It gets initialized along with the class initialization stage.
Example
We will try to implement a companion object and we will see how efficiently we can handle the memory management in Kotlin.
fun main(args: Array<String>) { // Accessing class variable and method //with out creating class object println("Hello!"+'
' + "This an example of accessing class variable without creating object." + MyClass.staticField+'
') println("Hello!"+'
' + "This an example of accessing class Method without creating an object." + MyClass.getStaticFunction()+'
'); } class MyClass{ companion object { val staticField = "This is a Static Variable." fun getStaticFunction(): String { return "This is a static Method." } } }
Output
Once you execute the code, it will generate the following output −
Hello! This an example of accessing class variable without creating object. This is a Static Variable. Hello! This an example of accessing class Method without creating an object. This is a static Method.
In this sample code, if you try to modify the value of any of the static variables, you will see Kotlin compiler will throw an error.
Example
fun main(args: Array) { // Trying to modify the static variable MyClass.staticField="Hello Students"; println("Hello!"+'
'+"This an example of accessing class variable with out creating object-"+MyClass.staticField+'
') } class MyClass{ companion object { val staticField = "This is an Static Variable" fun getStaticFunction(): String { return "This is a static Method" } } }
Output
The above piece of code will generate the following error −
$kotlinc -nowarn main.kt -include-runtime -d main.jar main.kt:5:5: error: val cannot be reassigned MyClass.staticField="Hello Students"; ^
Using object class and @JvmStatic annotation
As per the Kotlin documentation, once @JvmStatic annotation is applied upon any variable or method, it will work as static functionality of that class.
Example
In the following example, we will create an object class and in that object class, we will declare variables and methods using @JvmStatic annotation in order to implement static functionality in Kotlin environment.
fun main(args: Array) { // Accessing class variable and method //with out creating class object println("Hello!"+'
' + "This an example of accessing a class variable without creating an object." +MyClass.staticField+'
') println("Hello!"+'
' + "This an example of accessing a class Method without creating an object. " +MyClass.getStaticFunction()+'
'); } object MyClass{ @JvmStatic val staticField = "This is a Static Variable." @JvmStatic fun getStaticFunction(): String { return "This is a Static Method." } }
Output
It will generate the following output in the result section . −
Hello! This an example of accessing a class variable without creating an object. This is a Static Variable. Hello! This an example of accessing a class Method without creating an object. This is a Static Method.
- Related Articles
- What is the equivalent of Java static final fields in Kotlin?
- Kotlin static methods and variables
- What is the difference between non-static methods and abstract methods in Java?
- Static methods vs Instance methods in Java
- What's the Kotlin equivalent of Java's String[]?
- What are Class/Static methods in Java?\n
- Shadowing of static methods in Java\n
- Kotlin equivalent of Java's equalsIgnoreCase
- Are static methods inherited in Java?
- Java 8 static methods in interfaces
- What are Java methods equivalent to C# virtual functions?
- Static import the Math Class Methods in Java
- Can I overload static methods in Java?
- Can interfaces have Static methods in Java?
- Restrictions applied to Java static methods
