Tutorials Point, posted on 26th Apr, 2018

Groovy this method for Properties

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());
   }
}

Tutorials Point, posted on 26th Apr, 2018

Groovy Instance methods

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()); 
   } 
}

Tutorials Point, posted on 26th Apr, 2018

Groovy Method Return Values

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

Tutorials Point, posted on 26th Apr, 2018

Groovy Default Parameters within Parameters

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

Tutorials Point, posted on 26th Apr, 2018

Groovy Default Parameters

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

Tutorials Point, posted on 26th Apr, 2018

Groovy No Parameter

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();
   } 
}

Tutorials Point, posted on 26th Apr, 2018

Groovy No Parameter

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();
   } 
}

Tutorials Point, posted on 26th Apr, 2018

Groovy NodeBuilder

def nodeBuilder = new NodeBuilder() 

def studentlist = nodeBuilder.userlist {
   user(id: '1', studentname: 'John', Subject: 'Chemistry')
   user(id: '2', studentname: 'Joe', Subject: 'Maths')
   user(id: '3', studentname: 'Mark', Subject: 'Physics') 
} 

println(studentlist)

Tutorials Point, posted on 26th Apr, 2018

Groovy JsonOutput

import groovy.json.JsonOutput 
class Example {
   static void main(String[] args) {

      def output = JsonOutput.toJson([name: 'John', ID: 1])
      println(output);  
   }
}

Tutorials Point, posted on 26th Apr, 2018

Groovy Implicit Single Parameter

class Example {
   static void main(String[] args) {

      def clos = {println "Hello ${it}"};
      clos.call("World");
   } 
}

1 2 3 4 5 6 7 ... 17 Next
Advertisements
Loading...