+ 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?

28th Mar 2018, 1:57 PM
Ajay Prashanth
Ajay Prashanth - avatar
6 Antworten
+ 5
Please specify the programming language in your question.
28th Mar 2018, 2:23 PM
cHiRaG GhOsH
cHiRaG GhOsH - avatar
+ 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
28th Mar 2018, 2:26 PM
cHiRaG GhOsH
cHiRaG GhOsH - avatar
+ 2
Apologies, should have called it out. Was questioning about Java
28th Mar 2018, 2:24 PM
Ajay Prashanth
Ajay Prashanth - avatar
+ 2
Thank you!! I was just wondering if one way was considered more efficient/saving memory over the other
28th Mar 2018, 2:27 PM
Ajay Prashanth
Ajay Prashanth - avatar
+ 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 ^^
28th Mar 2018, 2:33 PM
Alex
Alex - avatar
+ 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...
28th Mar 2018, 3:28 PM
Heji Palaña
Heji Palaña - avatar