1
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.

JimS
  • 349
  • 1
  • 4
  • 19

2 Answers2

3

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.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

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 Window
  • System.out refers to the reference variable of the type PrintStream class
  • System Statically refers to a final class from java.lang package
  • out a reference to the PrintStream class and a static member of System class
  • println is a method of PrintStream class

Altogether,

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.

Community
  • 1
  • 1
candied_orange
  • 7,036
  • 2
  • 28
  • 62