System.out.println(((Window)this).size);
Say we have a class Window and the above command is written inside a method in a subclass. I want some help understanding what (Window)this does exactly. Where it refers to.
Suppose both the sub-class and the Window
class have a member called class. In this case, the sub-class's size
hides Window
's size
.
((Window)this).size
returns the size
member of the Window
class (assuming it's accssible), while this.size
will return the size
member of the sub-class.
System.out.println(((Window)this).size);
Can be broken down in steps as:
this
A reference to the current instance(Window)this
cast as the Window
class so non private Window
members are accessible(Window)this.size
Access the size member of this WindowSystem.out
refers to the reference variable of the type PrintStream classSystem
Statically refers to a final class from java.lang packageout
a reference to the PrintStream class and a static member of System classprintln
is a method of PrintStream classAltogether,
System.out.println(((Window)this).size);
will send this windows size to the console (provided out
hasn't been redirected) and follow it with a new line.