Non static method in Talend routines

All System routines are read only and has static methods that is why we are able to call all system methods with the class_name.method_name({arg},{arg2},…)

But to leverage full power of java in Talend platform we must know how to create routines with non static methods and how to create objects to call non static methods within Talend routines and in Job design.

Below example will demonstrate how to create a routine with non static methods and call non static methods in CalcUtil by creating objects of Calc Class.

Click Code > Routines > Create routine

Routine : Calc

package routines;

public class Calc {
	private static int a=0;
	private static int b=0;

	Calc()
	{
		 a=0;
		 b=0;
	}
	Calc(int a,int b)
	{
		 a=0;
		 b=0;
	}

	public static int getA() {
		return a;
	}

	public static void setA(int a) {
		Calc.a = a;
	}

	public static int getB() {
		return b;
	}

	public static void setB(int b) {
		Calc.b = b;
	}
	
	public int addition()
	{
		return a+b;
	}
	public int subtract()
	{
		return a-b;
	}
	public int multiply()
	{
		return a*b;
	}
	public int divide()
	{
		return a/b;
	}  
}

Click Code > Routines > Create routine

Routine : CalcUtil

package routines;

import routines.Calc;

public class CalcUtil {

	public static int add(int num1,int num2){
		Calc c = new Calc();
		Calc.setA(num1);
		Calc.setB(num2);
		return c.addition();
	}
	public static int sub(int num1,int num2){
		Calc c = new Calc(num1,num2);
		return c.subtract();
	}
	
}

Note* :  

  1. setA and setB are static methods of Calc routine and we are setting values by calling methods with class name.
  2. Calc.setA(num1);
    Calc.setB(num2);
  3. addition and subtract are non static methods to call non static methods we have created object of class Calc and called method using object.
  4. Calc c = new Calc();
    c.addition();
    c.subtract();

Click Job Designs > Create job 

Move tJava component from palette to designer and paste below print statement in code area of tJava.

System.out.println("Add 3+2="+CalcUtil.add(3,2));
System.out.println("Sub 3-2="+CalcUtil.sub(3,2));

Screenshot 2016-01-12 01.15.27.png

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s