Java and JavaScript

By abhirama

Recently I have been introduced to a lot of JavaScript and it would not be wrong to say I am in love with JavaScript. Also while coding in JavaScript I have got into the habit of comparing it with Java (which i have been using since i started my professional career).

A few days back I wrote a small script which needed to be used in three or four web pages and each time the script was called (on some browser event) I had to execute some other function conditionally i.e if the function being called was defined. In each of the web page only the other function to be called was different, the rest of the functionality remaining the same.

Eg:

function foo() {

//The conditional method calling part – START
if (self.bar) {

bar();

}

if (self.moo) {

moo();

}

.
.
.
.
.
.
//The conditional method calling part – END

//The common functionality – START

//The common functionality – END

}

The above script checks as to whether a function bar is defined and if so calls it. Same with function moo. As you can see in JavaScript checking whether a function has been defined is as simple as saying self(or window).functionName.

Q: How can we do something similar in Java (I have never come across a scenario where I had to do this in Java)?

Ans: Using reflection.

The below code calls the method foo if it is present.Else NoSuchMethodException is thrown.

public class MethodChk {

public void foo() {

System.out.println(“foo”);

}

public void bar() {

System.out.println(“bar”);

}

public static void main(String[] args) throws IllegalAccessException,

InvocationTargetException {

MethodChk methodChk = new MethodChk();
Class clz = methodChk.getClass();
try {

Method method = clz.getMethod(“foo”, new Class[] {});
method.invoke(new MethodChk(), new Object[] {});

} catch (NoSuchMethodException nsme) {

nsme.printStackTrace();

}
}

}
Java code becomes a bit clunky as Java does not treat functions (methods in Java world) as first class citizens. JavaScript does.

4 Responses to “Java and JavaScript”

  1. Pavitra Says:

    Hey Abhi

    That was a very good comparision.. I never knew we could do this in Java.But as you said i dont know if we ever need to this in Java unless the client is doubtful if the function is defined or not.But can you tell me one thing.. why are we passing an empty array as an argument?I did not get it’s purpose..
    Method method = clz.getMethod(”foo”, new Class[] {});
    method.invoke(new MethodChk(), new Object[] {});

    Pavitra

  2. abhirama Says:

    When we try to get a Method object using the getMethod method we have to pass along with the method name an array of its argument types.This is required as Java supports method overloading.

    Here since the method does not take any parameters we are passing an empty array.In case the method signature was as foo (String bar) then the code becomes:

    Method method = clz.getMethod(”foo”, new Class[] {String.class});
    method.invoke(new MethodChk(), new Object[] {“moo”});

  3. Pavitra Says:

    cool.. that makes it clear :)

  4. Sanket Singh Says:

    Hmm…And what is Java Script???
    Kya hai yeh sab???
    Write something that a common man like me is able to understand… :)

Leave a Reply