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.

Results 1 to 6 of 6

Thread: How do I call a method from the main method?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    10
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I call a method from the main method?

    How do I invoke a method from the main method? does that make sense?


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: How do I call a method from the main method?

    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.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    10
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I call a method from the main method?

    public class className{

    public void method1(int n) {

    //code
    }
    public static void main (String args[]) {
    //how do I call method1?
    }

  4. #4
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: How do I call a method from the main method?

    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";
    }

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: How do I call a method from the main method?

    Have a go at using the examples to call method1, let me know how you go

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How do I call a method from the main method?

    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)

Similar Threads

  1. Call method(s) within the same class
    By mwr76 in forum Object Oriented Programming
    Replies: 8
    Last Post: September 26th, 2011, 12:58 AM
  2. Beginner method call problem
    By Lasda in forum Java Theory & Questions
    Replies: 2
    Last Post: May 10th, 2011, 03:31 PM
  3. call super method outside of 'this'
    By questionmark in forum Object Oriented Programming
    Replies: 1
    Last Post: January 29th, 2011, 11:29 AM
  4. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM
  5. [SOLVED] Java exception "result already defined"
    By rptech in forum Object Oriented Programming
    Replies: 3
    Last Post: May 20th, 2009, 05:48 AM