Methods | SoloLearn: Learn to code for FREE!

0

Methods

can anyone explain methods I understand main method but can you make more??

12/31/2016 1:04:44 AM

Juan Madrigal

2 Answers

New Answer

+1

Yes. You can create unlimited amount of methods. Just remember to call them in the main one if you do. Here is a good link to learn a bit more about it for beginners: https://www.tutorialspoint.com/java/java_methods.htm Hope this helps.

0

Hello, First of all, yes, you can have as many methods as you want and they can have the most various functionalities. Mehtods are chuncks of code that can be called whenever you fancy. They can be used to avoid repetition or to organize your programme. All methods need to be called, which means that you need to make a reference to it. There are two basic ways of doing that: --Using the static keyowrd when creating the method: public STATIC void method(){} --Making a reference to the class where the method is: (Say you have a class called program) program var = new program(); var.method() When creating a method, you can use what is called an argument or parameter (or many, if you need to). Arguments are placed inside the NORMAL brackets and can be of any variable type (int, String, char etc.), even arrays. An example of a code using them would be: public void print(String x){ System.out.pirntln(x); } Methods can also RETURN some value back to an argumet. When you want to return something, place the type of the variable where the word 'static' is. An example of this is: public static INT (int x, int y){ RETURN x+y; } N.B. To start a method, you need to place the keywords, name of the method, brackets and curly brackets IN THAT ORDER Ex.: public static void (keywords) name(){ } Hope this helped you. :-)