Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Groovy method


May 14, 2021 Groovy


Table of contents


The method in Groovy is defined using the return type or using the def keyword. M ethod can receive any number of parameters. ic. odifiers such as public, private, and protected can be added. B y default, if no visibility modifier is provided, the method is public.

The easiest way is to have a method without parameters, as follows:

def methodName() { 
   //Method code 
}

Here's an example of a simple approach

class Example {
   static def DisplayName() {
      println("This is how methods work in groovy");
      println("This is an example of a simple method");
   } 
	
   static void main(String[] args) {
      DisplayName();
   } 
}

In the example above, DisplayName is a simple method consisting of two println statements that output some text to the console. In our static main method, we just call the DisplayName method. The output of the above method will be -

This is how methods work in groovy 
This is an example of a simple method

Method parameters

A method is usually useful if its behavior is determined by the value of one or more parameters. /b10> We can use method parameters to pass values to the called method. N ote that parameter names must be different from each other.

The simplest type of method to use parameters, as shown below

def methodName(parameter1, parameter2, parameter3) { 
   // Method code goes here 
}

The following is an example of a simple way to use parameters

class Example {
   static void sum(int a,int b) {
      int c = a+b;
      println(c);
   }  
	
   static void main(String[] args) {
      sum(10,5);
   } 
}

In this example, we create a sum method with 2 parameters a and b. B oth parameters are int types. Then we call the sum method from our main method and pass the values to the variables a and b.

Then we call the sum method from our main method and pass the values to the variables a and b.



The output of the above method will be a value of 15.

The default parameters

Groovy also has a rule to specify the default values for parameters in the method. I f there is no method for passing values to parameters, the default value is used. I f you use non-default and default parameters, it is important to note that the default parameters should be defined at the end of the parameter list.

Here's an example of a simple way to use parameters -

def someMethod(parameter1, parameter2 = 0, parameter3 = 0) { 
   // Method code goes here 
} 

Let's take a look at the same example we saw earlier for adding two numbers and creating a method with one default and one non-default parameter -

class Example { 
   static void sum(int a,int b = 5) { 
      int c = a+b; 
      println(c); 
   } 
	
   static void main(String[] args) {
      sum(6); 
   } 
}
In this example, we create a sum method with two parameters a and b. B oth parameters are int types. 5.
Therefore, when we call the sum method from the main method, we can choose to pass only one value of 6 and assign it to parameter a in the sum method.

The output of the above method will be a value of 11.

class Example {
   static void sum(int a,int b = 5) {
      int c = a+b;
      println(c);
   } 
	
   static void main(String[] args) {
      sum(6,6);
   } 
}

We can also call the sum method by passing 2 values, in the example above, we pass 2 values 6 and the second value 6 will actually replace the default value assigned to parameter b.



The output of the above method will be a value of 12.

Method returns a value

Method can also return the value to the caller. This is required in the current programming language, where the method performs some kind of calculation and then returns the desired value to the calling method.



The following is an example of a simple method with a return value.

class Example {
   static int sum(int a,int b = 5) {
      int c = a+b;
      return c;
   } 
	
   static void main(String[] args) {
      println(sum(6));
   } 
}

In our example above, note that this time we specify a return type of int for our method sum. I n the method, we use the return statement to send the sum value to the calling master. Since the value of the method is now available for the main method, we use the println function to display the value in the console.

In the previous example, we defined our methods as static methods, which means that we can access them directly from the class. it. e'll see the class in a later section, and now we'll show you how to use it.

The output of the above method will be a value of 11.

The instance method

Methods are typically implemented in classes in Groovy, just like the Java language. A class is simply a blueprint or template for creating different objects that define its properties and behavior. ss. herefore, the behavior is defined by creating methods in the class.

We'll look at the class in more detail later in the chapter, and here's an example of the method implementation in the class.

Here's an example of how to implement the method.

class Example { 
   int x; 
	
   public int getX() { 
      return x; 
   } 
	
   public void setX(int pX) { 
      x = pX; 
   } 
	
   static void main(String[] args) { 
      Example ex = new Example(); 
      ex.setX(100); 
      println(ex.getX()); 
   } 
}

In our example above, this time we didn't specify static properties for class methods. In our main function, we actually create an instance of the Example class and then call the method of the 'ex' object.



The output of the above method will be a value of 100.

Local and external parameter names

Groovy provides facilities that have local and global parameters like java. I n the following example, lx is a local parameter that has only scope within the getX() function, and x is a global property that can be accessed throughout the Example class. or.

class Example { 
   static int x = 100; 
	
   public static int getX() { 
      int lx = 200; 
      println(lx); 
      return x; 
   } 
	
   static void main(String[] args) { 
      println getX() 
   }  
}

When we run the program above, we get the following results.

200 
100

Method property

As in Java, groovy can use the this keyword to access its instance members. The following example shows that when we use the statement this.x, it references its instance and sets the value of x accordingly.

class Example { 
   int x = 100; 
	
   public int getX() { 
      this.x = 200; 
      return x; 
   } 
	
   static void main(String[] args) {
      Example ex = new Example(); 
      println(ex.getX());
   }
}

When we run the program above, we will get 200 results printed on the console.