Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // src/edu/umb/cs680/test/App.java
- package edu.umb.cs680.test;
- import edu.umb.cs680.test.other.Other;
- public class App {
- public static void main(String[] args) {
- Other someNumbers = new Other();
- int y = 2;
- // This lambda expression doesn't have acess to <Other.sort>'s local context
- // since its enclosing method is App.main
- // it can only access y not x
- // similarly, the enclosing class is App, not Other
- someNumbers.sort((a, b) -> {
- System.out.println(y);
- // System.out.println(x);
- return a - b;
- });
- }
- }
- // src/edu/umb/cs680/test/other/Other.java
- package edu.umb.cs680.test.other;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- public class Other {
- private ArrayList<Integer> numbers;
- public Other() {
- numbers = new ArrayList<Integer>();
- for (int i = 0; i < 10; i++) {
- numbers.add(Integer.valueOf(i));
- }
- }
- public void sort(Comparator<Integer> comparator) {
- int x = 1;
- Collections.sort(numbers, comparator);
- }
- }
Add Comment
Please, Sign In to add comment