Creating a method

creating a method

Creating a method in Java

For our understanding we do say creating a method but in Java it is actually be stated as defining a method. A class can have n number of methods. In previous lesson we have defined  a class called “Dog” and now we will insert some methods into this class

public Class Dog
{
    instance variables; 

    void bark()
     {
       local variables;
       statement1;
       statement2;

     }

     void barkWhenRobbed()
     {
          // set of instructions to execute
     }

}// class curly brace

Rules to define a method

  • Every method should have a return type. Here the return type is “void” , which means it returns nothing. you will learn more about return types in next chapters.
  • And then you have to name a method according to the java standards. In java it is always recommended to have a method name starting with small letter of the first word like “barkWhenRobbed()”
  • After defining a name you have to mention opening and closing curly braces  “()” . you will mention arguments if necessary inside this parenthesis.
  • Inside the curly braces ” { } ” of a method , you will define your code or statements which is also called as set of instructions.
  • In C-lang we have functions and in Java we have methods.
  • A method can also have arguments inside the method parenthesis. For example void myMethod(int x) { }

‘main()’ method in Java

In order run your program successfully , you have to mention a main method. Whenever you try to execute your program , JVM starts looking for the main method .

public static void main(String args[])
{

statement 1;
statement 2;
create object statement; 
call method bark();
call method barkWhenRobbed();

}

 

public static void main(String args[])

public : modifier – so everyone can access it

static : which means direct call. explained in the coming chapter

void : return type – void returns nothing

main : name of the method

args[] : array of type String (the square brackets [] indicates that it is an array)

The JVM runs everything inside the curly braces { } of the main method. Every Java application has to have at least one class and least one main method ( not one main per class , one main for application).

When we stick altogether , here is your sample program which explains the flow of your program.

public class Dog
{
   instance variables ;

  void bark() // (3a)
  {

   local variables ;
   statement 1;
   statement 2;
  // executes the above statements when this method is called.

  }//bark() method closed
  
  void barkWhenRobbed() // (4a)
   {
         local variables;
         statement 1;
         statement 2;
         statement 3;
    // executes the above statements when this method is called 
   }barkWhenRobbed() method closing brace

     public static void main(String args[]) // (1)

    {
   statement 1 creating an object dogobject for class Dog; (2)
   statement 2 calling the method bark() using the dogobject; (3)
   statement 3 calling the method barkWhenRobbed() using the dogobject; (4)
   } 

}// Dog class closing brace

 

In the above sample program , the JVM starts running by looking the main() method and executes all the statements inside the main() method. For your understanding this is just a sample program to explain how the JVM works inside a java program. you will execute a real simple complete Java program after the variables explanation.

Flow of the program

When you observe the sample program , you can see the numbers mentioned after the statements. Right after the program is loaded the JVM will flow accordingly.

1 : JVM will find the main to start executing the program

2 : JVM will then creates an object for class Dog

3 : This statement tells the JVM to go to the bark() method and start executing the statements inside the bark() method.

3a : The JVM will enter into the bark() method and it starts executing the statements inside the bark() method  untill the closing brace is found then the JVM returns back to the main() method to continue the program

4 : And this statement tells the JVM to go to the barkWhenRobbed() method

4a : The JVM enters into the barkWhenRobbed() method and it starts executing the statements until the closing brace is found. After finding the closing brace then the JVM returns back to the main() method

This is how the JVM will flow through out the program. Closing brace ” } ” is nothing but a break to that method.

“Slow and Steady wins the race” , don’t be in a hurry to learn in an overnight.

 

2 thoughts on “Creating a method

Leave a Reply

Your email address will not be published. Required fields are marked *