Wednesday, 1 June 2016

keyword 'super' and 'this'


Both refer to instance(object) members(variables and methods). So you cannot use them in a static context.

'this' refers to current instance itself, including inherited parts. (1) it is used to distinguish instance variable from the local variable or method parameters. (2) it is used to refer to another constructor of the same class. (3) this refers to instance members inherited from the super classes, if they are not hidden or overridden.

Please note: on (2), you must use 'this' to refer to other overloaded constructors in a constructor; you cannot seating within a constructor then invoking another overloaded constructor by its name.

'super' refers to current instance's super-class instance; you can only access one more layer higher. (1) in the first line of the constructor to invoke its super-class constructor. (2) when super class's instance members are hidden in child class; then you may use super keyword to refer them in the sub-class.

please note: calling of super() and this() must be in the first line of constructor.

Call of super class
If there is no super constructor present in the sub-class constructor, jvm will automatically insert a non-argument, super(), in the first line of constructor. so you need to watch out here, if you explicitly declare an constructor with argument in the super class alone; then you will get a compiling error,

No comments:

Can Jackson Deserialize Java Time ZonedDateTime

Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...