4

We know that a lambda expression can refer to and use static instance variables, instance variables and local variables( if they are effectively final). All this seems ok. Whenever I see any session on Lambdas and Java's take on functional programming I see one common point which is "Writing concurrent code is tough and so adapting functional code helps". However if I can access Static and instance variables in a lambda does this not defeat this point altogether. I know we have parallel streams which are very helpful in certain concurrent case but still is the closure scope not broken in Java if we are going towards functional programming style.

Also we should be creating pure functions in a functional style of programming. However if I am dependent on the state of the instance then my function is not pure.

I might be incorrect in inferring what I have shared but if I am then was there a particular reason to allow these design principles.

public class UsingStaticVariables {

    static int staticTest = 10;

    public static void main(String args[]) {

        staticTest++;
        System.out.println("Static test first value is " + staticTest);
        Supplier<Integer> supplier = () -> {return staticTest++; };
        staticTest++;
        System.out.println("Static test second value is " + staticTest);
        System.out.println("Static Test lambda output is " + supplier.get());

    }
}
2
  • 7
    "Adapting functional code" and accessing (or, to be specific, modifying) static or instance variables are incompatible. Just using lambda expressions doesn't automatically mean you're using functional programming; another level of discipline is required. Commented May 24, 2018 at 18:27
  • You can make the lambda expression even simpler: Supplier<Integer> supplier = () -> staticTest++; Commented May 25, 2018 at 8:35

1 Answer 1

6

if I can access Static and instance variables in a lambda does this not defeat this point altogether[?]

The ability for lambdas to access static and instance variables does not make the whole feature fail at providing an approach to functional programming. Only when lambdas actually do access such data is pure functional programming violated.

But also, it sounds like you may be working from a false premise. Lambdas provide for more convenient representations of higher-order functions than Java previously had, but there is no reason to take that as a sign of Java moving toward being a pure functional language. I am confident that that will never happen.

I know we have parallel streams which are very helpful in certain concurrent case but still is the closure scope not broken in Java if we are going towards functional programming style.

Nothing requires you to make your lambdas touch anything they cannot reach via their arguments. And avoiding that is a very good idea in lambdas that are going to be shared between threads, but even that doesn't protect you from all the intricacies of multithreaded programming.

Java supports -- even more so nowadays -- multiple programming paradigms and mixed programming paradigms. This is generally a good thing for programmers, which is probably why a lot of programming languages seem to move in that direction, regardless of where they start.

[W]as there a particular reason to allow these design principles[?]

The motivation for most of Java language and standard library development has pretty much always been to make the language easier to use in a variety of ways, for as many programming tasks as possible. Lambdas seem to have been motivated much more by a desire to reduce the importance of anonymous inner classes than by any ideal of providing better support for functional programming style. The FP angle largely comes along for the ride, though the standard library does seem to have really latched on to that.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks John for explaining the concepts in detail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.