What is the equivalent of Java static methods in Kotlin?

前端 未结 28 944
眼角桃花
眼角桃花 2020-11-27 09:03

There is no static keyword in Kotlin.

What is the best way to represent a static Java method in Kotlin?

相关标签:
28条回答
  • 2020-11-27 09:54

    There is no static keyword in kotlin. kotlin docs recommends to use package-level functions if u want to follow DRY. Create a file with .kt extension and put your method in it.

    package p
        fun m(){
        //fun body
        }
    

    after compilation m will have a signature of public static final void

    and

    import p.m
    

    0 讨论(0)
  • 2020-11-27 09:57

    A. Old Java Way :

    1. Declare a companion object to enclose a static method / variable

      class Foo{
      companion object {
          fun foo() = println("Foo")
          val bar ="bar"  
          }
      }
      
    2. Use :

      Foo.foo()        // Outputs Foo    
      println(Foo.bar) // Outputs bar
      


    B. New Kotlin way

    1. Declare directly on file without class on a .kt file.

      fun foo() = println("Foo")
      val bar ="bar"
      
    2. Use the methods/variables with their names. (After importing them)

      Use :

      foo()        // Outputs Foo          
      println(bar) // Outputs bar     
      

    0 讨论(0)
  • 2020-11-27 09:57

    Use @JVMStatic Annotation

    companion object {
    
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
                EditProfileFragment().apply {
                    arguments = Bundle().apply {
                        putString(ARG_PARAM1, param1)
                        putString(ARG_PARAM2, param2)
                    }
                }
    }
    
    0 讨论(0)
  • 2020-11-27 09:59

    Docs recommends to solve most of the needs for static functions with package-level functions. They are simply declared outside a class in a source code file. The package of a file can be specified at the beginning of a file with the package keyword.

    Declaration

    package foo
    
    fun bar() = {}
    

    Usage

    import foo.bar
    

    Alternatively

    import foo.*
    

    You can now call the function with:

    bar()
    

    or if you do not use the import keyword:

    foo.bar()
    

    If you do not specify the package the function will be accessible from the root.

    If you only have experience with java, this might seem a little strange. The reason is that kotlin is not a strictly object-oriented language. You could say it supports methods outside of classes.

    Edit: They have edited the documentation to no longer include the sentence about recommending package level functions. This is the original that was referred to above.

    0 讨论(0)
  • 2020-11-27 09:59
    object objectName {
        fun funName() {
    
        }
    }
    
    0 讨论(0)
  • 2020-11-27 09:59

    Simply you need to create a companion object and put the function in it

      class UtilClass {
            companion object {
      //        @JvmStatic
                fun repeatIt5Times(str: String): String = str.repeat(5)
            }
        }
    

    To invoke the method from a kotlin class:

    class KotlinClass{
      fun main(args : Array<String>) { 
        UtilClass.repeatIt5Times("Hello")
      }
    }
    

    or Using import

    import Packagename.UtilClass.Companion.repeatIt5Times
    class KotlinClass{
      fun main(args : Array<String>) { 
         repeatIt5Times("Hello")
      }
    }
    

    To invoke the method from a java class:

     class JavaClass{
        public static void main(String [] args){
           UtilClass.Companion.repeatIt5Times("Hello");
        }
     }
    

    or by adding @JvmStatic annotation to the method

    class JavaClass{
       public static void main(String [] args){
         UtilClass.repeatIt5Times("Hello")
       }
    }
    

    or both by adding @JvmStatic annotation to the method and making static import in java

    import static Packagename.UtilClass.repeatIt5Times
    class JavaClass{
       public static void main(String [] args){
         repeatIt5Times("Hello")
       }
    }
    
    0 讨论(0)
提交回复
热议问题