What is a variable? A variable is a storage for a value, which can be a string, a number, or something else. Every variable has a name (or an identifier) to distinguish it from other variables. You can access a value by the name of the variable. Declaring variables Kotlin provides two keywords val (for value) declares an immutable variable (just a named value or a constant ), which cannot be changed after is has been initialized. var (for variable) declares a mutable variable , which can be changed (as many times as needed) Both val and var keywords provide you a variable! To assign a certain value to a variable, we should use the assignment operator = It is also possible to declare and initialize a variable with the value of another variable. Storing different types of values There is one restriction for mutable variables (the ones declared with the keyword var), though. When reassigning their values, you can only use new values of the same type as the initial one. For implemen...
Overview If you want to write some functions or properties of a class and you want to use it without creating an instance of that class, then you should write these functions and properties in a companion object. Companion object is similar to Singleton object. All the functions and properties in a companion object are static. Object vs Companion Object Object can be used when you don't want to write some static functions or properties inside a class. Companion object can be used when you want to write some functions or properties of a class as static. Do's and Dont's You can give a name to a companion object but it's of no use. class ToBeCalled { companion object Test { var someInteger : Int = 10 fun callMe () = println ( "You are calling me :)" ) } } fun main (args: Array<String>) { print (ToBeCalled. someInteger ) } Companion object can be created outside a class. Reference https://blog.mindorks.com/companion-object-i...
Comments
Post a Comment