'Static' methods cannot use 'this' keyword.
3. Create 'static' specified method 'setDimensions( )' containing the parameters same as instance variable of this class (i.e. width, height and depth) and this 'static' specified method tries to use 'this' to differentiate the instance variables from the parameters of this method as shown below:
Observe that errors are displayed in the statements that are using 'this' keyword
After looking at this error 'Cannot use this in static context', its very clear that we cannot use 'this' keyword in a static method.
Download this project:
Click here to download this project containing class files used in this post (You can download this project and import into Eclipse IDE on your machine)
Please comment below to feedback or ask questions.
'static' block will be explained with an example in next post.
'static' block will be explained with an example in next post.
2 comments:
What is the reason behind it ?
this is used to refer to the parent object of a variable or method. When you declare static on a method the method can be called without needing to instantiate an object of the class. Therefore the this keyword is not allowed because your static method is not associated with any objects.
Post a Comment