methods | SoloLearn: Learn to code for FREE!



why cant we define the method name and definition inside the main function and use it and execute ...

7/9/2019 9:46:12 AM


3 Answers

New Answer


We can't use method definaion in main. You should know the purpose of method is to reuse the method code anytime. According to the rules of syntax, we must define the method outside the main.


in c++, method has an another name called member function. it has to be defined inside of class and it can be reused by the instances of that class. otherwise it's going to throw a syntax error.


Java does not support nested methods. However, there are indirect ways to write a method in a method: