Could anyone please explain what is Idempotent method in Java?
After google, what I found about this is Idempotent methods are methods, which are written in such a way that repeated calls to the same method with the same arguments yield same results. And am not getting any example to clear the concept of it.
Every method if called with same arguments will yield same result. Then what is the difference between Idempotenet method and normal method?
Thanks in advance...

Consider these two methods:
int sum(int a, int b) idempotent - every call gives same result
void writeToDatabase(String a) not. database gets bigger with every call

or

String toString() idempotent
int getNextPrimeNumber() each call gives a different result

Could anyone please explain what is Idempotent method in Java?
After google, what I found about this is Idempotent methods are methods, which are written in such a way that repeated calls to the same method with the same arguments yield same results. And am not getting any example to clear the concept of it.
Every method if called with same arguments will yield same result. Then what is the difference between Idempotenet method and normal method?
Thanks in advance...

How about:

Money tenDollarsUSD = fiveDollarsUSD.add(fiveDollarsUSD);

or

Month february = january.add(oneMonth);

Idempotent, both.
5$ + 5$ is always 10$
jan + 1 month is always feb

Idempotent methods are also called pure methods; methods whose output purely depends on the passed in arguments and not any external state. Pure functions play a critical role in "pure functional languages". The wikipedia entry for pure functions describes them pretty well:

The function always evaluates the same result value given the same argument value(s). The function result value cannot depend on any hidden information or state that may change as program execution proceeds or between different executions of the program, nor can it depend on any external input from I/O devices

Examples:

// Pure
public int add(int a, int b) {
  // computation uses only the passed in arguments
  // all resources used are exclusive to the function stack
  // on which they were created (except shared global resources)
  int result = a + 10 - 233 * 23 / 2 + b;
  return result;
}

// Impure
public int add(int a, int b) {
  // computation uses global state; not pure
  int result = a + 10 - 233 * 23 / 2 + b;
  result = result * CommonConstants.MODIFIER;
  return result;
}

~s.o.s~, is the CommonConstants.MODIFIER meant to be changeable at runtime, that is "state that may change as the program execution proceeds .."? Is it mutable? Is it state?

Yes.

In most applications, a common class instance (singleton) is used to keep track of global state. The advantage here is ease of use; just refer that class from any part of the application. The downfall is that tracking *which* part of your code changed the *global* state is difficult for an application having sizeable application logic.

In Java, it is a commonplace occurrence to rely on instance fields to be used in class methods. In languages where functions are the only units of abstraction and complex abstractions are built upon simple ones, making a function relying on some global piece of information limits its reuse.

Thanks for everyone for help.
Now I can understabd it...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.