How do I invoke a method from the main method? does that make sense?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
How do I invoke a method from the main method? does that make sense?
You us the method name and argument pattern to refer to it, e.g:
public static void main(String[] args){ //Will work someMethod("Hello World"); //Wont work someMethod(); } private void someMethod(String message){ System.out.println("In someMethod, message = " + message); }
Have a look at this and the next few tutorial pages:
Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
You may also want to look in the tutorial section of this forum, people have written up some very good tutorials which should help.
public class className{
public void method1(int n) {
//code
}
public static void main (String args[]) {
//how do I call method1?
}
You use the method name, followed by parenthesis "()". You put arguments in the brackets, separated by a comma.
A method is made up of several parts, lets look at:
public void aMethod(String message) { //do stuff }
First off, public: This is the modifier, namely the access modifier. This determines who has access to the method. To begin with, just make everything public so you don't run into any trouble. Other modifiers are things such as static, final, abstract, etc. but don't worry about them for now.
Modifiers: Retrieving and Parsing Method Modifiers (The Java™ Tutorials > The Reflection API > Members)
void: This is the return type, each method can only return one variable of a specific data type. void means the method dos not return anything. in place of void, you could put int, String, double, or any other data type (including your own custom classes). If you use anything other than void, your method must have a return statement
public String aMethod() { return "Hello World"; }
For returning a value from a method, see: Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
aMethod(): This is the method name and what you use to refer to the method. If the method receives arguments, you need to put them in the parenthesis with the data type then variable name. If the method receives multiple args, you use a comma to separate them. You must pass args to a method in the same order which it receives them, e.g
public void myMethod(String message, int number){} public static void main(String[] args){ // calling myMethod with a String then an int will work myMethod("Hello", 5"); // calling myMethod with an int, then a string wont myMethod(5, "Hello") }
Passing values to methods: Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
{
//some code
}:
Your code goes in between the curly braces
put it together:
public static void main(String[] args){ someMethod("Hello World"); anotherMethod(1, "someText"); String myText = aThirdMethod(); } public void someMethod(String message){ System.out.println("In someMethod, message = " + message); } public void anotherMethod(int quantity, String message){ System.out.println("In anotherMethod, message = " + message + " quantity = " + quantity); } public String aThirdMethod(){ System.out.println("In aThirdMethod"); return "Some meaningful text"; }
Have a go at using the examples to call method1, let me know how you go
You didn't declare method1 as being static. If a method is not static, you must have an instance of that object to call that method. If it is static, you don't.
See: Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)