Skip to main content
removing the extraneous "." in order for the code to actually compile
Source Link

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() // OutputOutputs Foo println(Foo.bar) .  // OutputOutputs Barbar 


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() // OutputOutputs Foo  println(bar) .  // OutputOutputs Barbar  

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() // Output Foo println(Foo.bar) .  // Output 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() // Output Foo println(bar) .  // Output Bar 

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  

added 772 characters in body
Source Link
erluxman
  • 19.6k
  • 22
  • 101
  • 129

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() } // Output Foo   println(Foo.bar) . // }Output Bar 

2. Use :

 
 Foo.foo() // Output Foo println(Foo.bar) . // Output Bar 
 

B. New Kotlin way

  1. Create .kt Declare directly on file inside a package (Let's say com.example.foo). You don't need awithout class anymore to haveon a variable/method to behave like static in Java. Methods, variables are also first-class citizen in Kotlin.

  2. Declare methods/variables.kt as package level (Not enclosed inside a class & written directly on .kt file)file.

     fun foo() = println("Foo")  val bar ="bar" 
  3. Import the class you just created to the file where you want to use these methods/variables import com.example.foo.*

  4. Now you can use theUse the methods/variables with their names. (After importing them)

    Use :

     foo() // Output Foo  println(bar) . // Output Bar 

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() // Output Foo println(Foo.bar) . // Output Bar 

B. New Kotlin way

  1. Create .kt file inside a package (Let's say com.example.foo). You don't need a class anymore to have a variable/method to behave like static in Java. Methods, variables are also first-class citizen in Kotlin.

  2. Declare methods/variables as package level (Not enclosed inside a class & written directly on .kt file).

    fun foo() = println("Foo") val bar ="bar" 
  3. Import the class you just created to the file where you want to use these methods/variables import com.example.foo.*

  4. Now you can use the methods/variables with their names.

    Use :

    foo() // Output Foo println(bar) . // Output Bar 

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()  // Output Foo   println(Foo.bar) . // Output 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() // Output Foo  println(bar) . // Output Bar 

added 772 characters in body
Source Link
erluxman
  • 19.6k
  • 22
  • 101
  • 129

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" } } 

Use a companion object to enclose a static method2. Use / variable:

class Foo{ companion object {  fun foo() = println("Foo") val bar ="bar" } } 

Use:

Foo.foo() // Output Foo  println(Foo.bar) . // Output Bar 

B. New Kotlin way

  1. Create .kt file inside a package (Let's say com.example.foo). You don't need a class anymore to have a variable/method to behave like static in Java. Methods, variables are also first-class citizen in Kotlin.

  2. Declare methods/variables as package level (Not enclosed inside a class & written directly on .kt file).

    fun foo() = println("Foo") val bar ="bar" 
  3. Import the class you just created to the file where you want to use these methods/variables import com.example.foo.*

  4. Now you can use the methods/variables with their names.

    Use :

    foo() // Output Foo println(bar) . // Output Bar 

A. Old Java Way :

Use a companion object to enclose a static method / variable

class Foo{ companion object {  fun foo() = println("Foo") val bar ="bar" } } 

Use:

Foo.foo() // Output Foo println(Foo.bar) . // Output Bar 

B. New Kotlin way

  1. Create .kt file inside a package (Let's say com.example.foo). You don't need a class anymore to have a variable/method to behave like static in Java. Methods, variables are also first-class citizen in Kotlin.

  2. Declare methods/variables as package level (Not enclosed inside a class & written directly on .kt file).

    fun foo() = println("Foo") val bar ="bar" 
  3. Import the class you just created to the file where you want to use these methods/variables import com.example.foo.*

  4. Now you can use the methods/variables with their names.

    foo() // Output Foo println(bar) . // Output Bar 

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() // Output Foo  println(Foo.bar) . // Output Bar 

B. New Kotlin way

  1. Create .kt file inside a package (Let's say com.example.foo). You don't need a class anymore to have a variable/method to behave like static in Java. Methods, variables are also first-class citizen in Kotlin.

  2. Declare methods/variables as package level (Not enclosed inside a class & written directly on .kt file).

    fun foo() = println("Foo") val bar ="bar" 
  3. Import the class you just created to the file where you want to use these methods/variables import com.example.foo.*

  4. Now you can use the methods/variables with their names.

    Use :

    foo() // Output Foo println(bar) . // Output Bar 

added 772 characters in body
Source Link
erluxman
  • 19.6k
  • 22
  • 101
  • 129
Loading
deleted 1 character in body
Source Link
Gastón Saillén
  • 13.3k
  • 5
  • 75
  • 82
Loading
deleted 104 characters in body
Source Link
erluxman
  • 19.6k
  • 22
  • 101
  • 129
Loading
Corrected grammar, fixed overwhelming bold formatting.
Source Link
Loading
added 55 characters in body
Source Link
erluxman
  • 19.6k
  • 22
  • 101
  • 129
Loading
deleted 101 characters in body
Source Link
erluxman
  • 19.6k
  • 22
  • 101
  • 129
Loading
added 143 characters in body
Source Link
erluxman
  • 19.6k
  • 22
  • 101
  • 129
Loading
added 58 characters in body
Source Link
erluxman
  • 19.6k
  • 22
  • 101
  • 129
Loading
Source Link
erluxman
  • 19.6k
  • 22
  • 101
  • 129
Loading