In JAVA CompletableFuture is a class which contains a static method named supplyAsync() I can call this static method using CompletableFuture.supplyAsync() pattern but I am unable to call the static method on object of the same class CompletableFuture.

Example:

CompletableFuture c = new CompletableFuture();
c.supplyAsync() //->doesnt work cant recognize the method supplyAsync!

My doubt is why cant we call static method on object of the same class?

I was expecting the supplyAsync method to be called on object of class CompletableFuture.

1

There are 1 best solutions below

0
neil lee On

My doubt is why cant we call static method on object of the same class?

It's wrong. You can call static methods on objects like this,

assert 3 == new CompletableFuture<>()
          .supplyAsync(() -> 1)
          .thenCombine(CompletableFuture.supplyAsync(() -> 2), Integer::sum)
          .join();

But you shouldn't do this. Static methods are class-level methods, they should not be called on an object.