+ 2
Where do you define methods in Java
Is declaring methods before the main more efficient or does it make no difference whether we define it before or after the call?
6 ответов
+ 5
Please specify the programming language in your question.
+ 4
To answer the question , it makes practically no difference where you declare your methods. You may declare then before or after the main method
+ 2
Apologies, should have called it out. Was questioning about Java
+ 2
Thank you!! I was just wondering if one way was considered more efficient/saving memory over the other
+ 2
edit: written before you specified Java :P
In c++ your code doesn't even compile if you don't declare it before calling. You can declare a prototype and implement the actual code after the call though.
The efficiency is the same, since the compiler places a placeholder where the function should be and the linker puts the actual code in it (a bit simplified, but enough).
In the end the code your device runs is the same no matter where the code in your project is written.
For uncompiled languages someone else needs to give you informations, because I don't enough about the process to help you ^^
+ 1
inside the class and outside the main method...
public class sampleclass{
public void yourMethod(){
//to do here
}
publis static void main(String[] args){
//to do here
}
}
in my understanding you cannot define a method inside the main method. Correct me.if I'm wrong. And calling a method before its definition will cause an error...