Skip to content

Instantly share code, notes, and snippets.

@nipafx
Created May 11, 2016 21:34
Show Gist options
  • Save nipafx/264e9a705cba15766d66eed4d3549389 to your computer and use it in GitHub Desktop.
Save nipafx/264e9a705cba15766d66eed4d3549389 to your computer and use it in GitHub Desktop.
Demonstrating Visibility
package org.codefx.lab.visibility;
class PackagePrivate {
public String publicToUpper(String s) {
return s.toUpperCase();
}
String packageToUpper(String s) {
return s.toUpperCase();
}
}
package org.codefx.lab.visibility;
public class Public extends PackagePrivate { }
package org.codefx.lab.visibility.not;
import org.codefx.lab.visibility.Public;
import org.junit.Test;
import java.util.stream.Stream;
public class VisibilityTest {
@Test
public void toUpper() throws Exception {
Public p = new Public();
Stream.of("a", "b", "c")
.map(p::publicToUpper)
// next line is a compile error:
// "The type org.codefx.lab.visibility.Public does not define packageToUpper(java.lang.String)
// that is applicable here"
// commenting it out makes the code compile and run
.map(p::packageToUpper)
.forEach(System.out::println);
}
}
@danieldietrich
Copy link

This reproduces the error:

public class VisibilityTest {

    @Test
    public void toUpper() throws Exception {
        final BiFunction<Public, String, String> publicStringStringBiFunction = Public::publicToUpper;

        // java.lang.IllegalAccessError: tried to access class org.codefx.lab.visibilityPackagePrivate from class org.codefx.lab.visibility.not.VisibilityTest
        publicStringStringBiFunction.apply(new Public(), "a");
    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment