A method is in Groovy is defined with a return type or with the def
keyword. Methods can receive any number of arguments. It’s not
necessary that the types are explicitly defined when defining the
arguments. Modifiers such as public, private and protected can be added.
By default, if no visibility modifier is provided, the method is
public.
The simplest type of a method is one with no parameters as the one shown below −
The simplest type of a method with parameters as the one shown below −
The output of the above method would be the value 15.
Following is an example of simple method with parameters −
The output of the above method would be the value 11.
The output of the above method would be the value 12.
Following is an example of simple method with a return value.
The output of the above method would be the value 11.
We will see classes in more detail in a later chapter but Following is an example of a method implementation in a class. In our previous examples we defined our method as static methods which meant that we could access those methods directly from the class. The next example of methods is instance methods wherein the methods are accessed by creating objects of the class. Again we will see classes in a later chapter, for now we will demonstrate how to use methods.
Following is an example of how methods can be implemented.
The output of the above method would be the value 100.
The simplest type of a method is one with no parameters as the one shown below −
def methodName() { //Method code }Following is an example of simple method
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 above example, DisplayName is a simple method which consists of two println statements which are used to output some text to the console. In our static main method, we are just calling the DisplayName method. The output of the above method would be −
This is how methods work in groovy This is an example of a simple method
Method Parameters
A method is more generally useful if its behavior is determined by the value of one or more parameters. We can transfer values to the called method using method parameters. Note that the parameter names must differ from each other.The simplest type of a method with parameters as the one shown below −
def methodName(parameter1, parameter2, parameter3) { // Method code goes here }Following is an example of simple method with 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 are creating a sum method with 2 parameters, a and b. Both parameters are of type int. We are then calling the sum method from our main method and passing the values to the variables a and b.
The output of the above method would be the value 15.
Default Parameters
There is also a provision in Groovy to specify default values for parameters within methods. If no values are passed to the method for the parameters, the default ones are used. If both nondefault and default parameters are used, then it has to be noted that the default parameters should be defined at the end of the parameter list.Following is an example of simple method with parameters −
def someMethod(parameter1, parameter2 = 0, parameter3 = 0) { // Method code goes here }Let’s look at the same example we looked at before for the addition of two numbers and create a method which has one default and another 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 are creating a sum method with two parameters, a and b. Both parameters are of type int. The difference between this example and the previous example is that in this case we are specifying a default value for b as 5. So when we call the sum method from our main method, we have the option of just passing one value which is 6 and this will be assigned to the parameter a within the sum method.
The output of the above method would be the value 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 our example above we are passing 2 values of 6. The second value of 6 will actually replace the default value which is assigned to the parameter b.
The output of the above method would be the value 12.
Method Return Values
Methods can also return values back to the calling program. This is required in modern-day programming language wherein a method does some sort of computation and then returns the desired value to the calling method.Following is an example of 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 above example, note that this time we are specifying a return type for our method sum which is of the type int. In the method we are using the return statement to send the sum value to the calling main program. Since the value of the method is now available to the main method, we are using the println function to display the value in the console.
The output of the above method would be the value 11.
Instance methods
Methods are normally implemented inside classes within Groovy just like the Java language. A class is nothing but a blueprint or a template for creating different objects which defines its properties and behaviors. The class objects exhibit the properties and behaviors defined by its class. So the behaviors are defined by creating methods inside of the class.We will see classes in more detail in a later chapter but Following is an example of a method implementation in a class. In our previous examples we defined our method as static methods which meant that we could access those methods directly from the class. The next example of methods is instance methods wherein the methods are accessed by creating objects of the class. Again we will see classes in a later chapter, for now we will demonstrate how to use methods.
Following is an example of how methods can be implemented.
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 above example, note that this time we are specifying no static attribute for our class methods. In our main function we are actually creating an instance of the Example class and then invoking the method of the ‘ex’ object.
The output of the above method would be the value 100.
Local and External Parameter Names
Groovy provides the facility just like java to have local and global parameters. In the following example, lx is a local parameter which has a scope only within the function of getX() and x is a global property which can be accessed inside the entire Example class. If we try to access the variable lx outside of the getX() function, we will get an error.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 above program, we will get the following result.
200 100
this method for Properties
Just like in Java, groovy can access its instance members using the this keyword. The following example shows how when we use the statement this.x, it refers to 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 above program, we will get the result of 200 printed on the console.
No comments:
Post a Comment