Pages

Saturday, June 16, 2018

Learn Kotlin 11 (Object declaration,properties and fields )

Object Declaration

We have heard about Singleton . which is useful in situations like creating facade objects and kotlin makes it easy declare singleton as here
object HomeManager{
fun registerHomeProvider(provider:HomeProvider){
//....
}
val allHomeProviders:Collection<HomeProvider>
get() = //.....
}
This is called an object declaration and it always has a name following the object keyword just like variable declaration  an object declaration is not an expression and cannot be used on the right hand side of an assignment statement object declarations initialization is thread safe to refer to an object we use its name directly like given below
HomeManager.registerHomeProvider{.....}
Such objects can have supertypes to like given below as
object DefaultListener:MouseAdapter(){
 override  fun mouseClicked (e:MouseEvent){
 //......
}
 override fun mouseEntered(e:MouseEvent){}{
//........
}
}
The points to remember here are that object declaration can't be local (be nested directly inside the function ) they can also be nested into other object declarations or non inner classes

Companion Objects

And in kotlin like in other languages classes do not have static methods in most cases its recommended to simply use package level function instead
And if you need to write function that can be called without having class instance but needs access to the internals of a class for example a factory method you can write it as a member of an object declaration inside class as defined above
and more specifically if you declare a companion object inside your class you can call its members as same as static methods in java /C# like given below

class NadsClass {
companion object Factory{
 fun create():NadsClass = NadsClass()
}
  }
Members of the companion object can be called by using simply the class name as the qualifier as given below
val instance = NadsClass.create()
As the members of companion objects look like static members in other languages ,Those are still instance members of real objects and can for example implement interfaces  and one thing to remember here is that the name of the companion object can be ignored in which the name Companion will be used as for example
interface Factory <T>{
fun create():T
}
class NadsClass{
companion object : Factory<NadsClass>{
override fun create (): NadsClass = NadsClass()

}


There is a way in which on JVM you can have members of companion objects generated as real static methods and fields if you use the @JvmStatic annotation to get more details see java interoperability

Properties and fields

Backing Fields
The backing field cannot be declared  directly in kotlin classes when a property needs a backing field kotlin provides it automatically this backing fields are referenced in the accessors using the field identifier
 
var count = 0
//remember the initializer assigns the backing field directly
set(value){
if (value >=0) field = value
}
The field identifier can only be used in the accessors of the property and the backing fields are generated for a property if it uses the default implementation of at least one of the accessors or if a custom accessor references it through the field identifier
here is an example with no backing field

val isEmpty :Boolean
get() = this.size == 0

Backing property 

To do something that does not fit into this implicit backing field scheme you can always fall back to having a backing property as :
 
private var _table: Map<String,Int>? = null
public val table : Map <String,Int>
get(){
if(_table == null ){
//Type parameter are infered
__table == HashMap()
}
return __table ?: throw AssertionError ("set to null by another thread")
}

This is just the same as in java since access to private properties with default getters and setters is optimized so that no function call overhead is introduced


Thats all for this post guys hope you like this post comment your suggestions and doubts in the comment section and don't forget to share and follow and to get latest lessons and updates on this blog follow by email id 

No comments:

Post a Comment