Lets find out what happens when a 'static' method calls a method which is not declared as static.
Lets implement this on Eclipse IDE:
1. Create 'StaticMethodCalling' class under any project as shown below:
Download this Project:
Click here to download the project containing the class file used in this post (You can download this project and import into Eclipse IDE on your machine)
Lets find out what happens when a 'static' method calls other 'static' method
Lets implement this on Eclipse IDE:
1. In the above project, open the 'StaticMethodsCalling' class and specify 'static' before the non-static method 'sample( )' as shown below and save:
2. Observe that the error displayed in the step 4 of above project is now fixed as shown below:
3. Create another class 'AccessStaticMethods' with main( ) method as shown below:
6. Observe that the output is displayed in console as shown below:
We've called the 'staticSample( )' static method from 'AccessStaticMethods' class without creating object and 'sample( );' calling statement inside the 'staticSample( )' called the 'static' specified method 'sample( )' and executed the print statement inside the 'sample( )' method.
Download this project:
Click here to download the project containing the class files used in this post (You can download and import into Eclipse IDE on your machine)
Please comment below to feedback or ask questions.
'static' methods can only access 'static' instance variables will be explained with an example in next post.
No comments:
Post a Comment