Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PhoneNumber(String phoneNumber) {
- numberLength = 7; // <== assignment to numberLength
- String currentNumber = phoneNumber.replaceAll(
- regularExpression, "");
- if (currentNumber.length() == numberLength)
- formattedPhoneNumber = currentNumber;
- else
- formattedPhoneNumber = null;
- }
- public class Foo {
- public void baz(int bar) {
- // While the next line is commented, bar is effectively final
- // and while it is uncommented, the assignment means it is not
- // effectively final.
- // bar = 2;
- }
- }
- final int variable = 123;
- int variable = 123;
- variable = 456;
- int variable = 123;
- final int number;
- number = 23;
- int number;
- number = 34;
- @FunctionalInterface
- interface IFuncInt {
- int func(int num1, int num2);
- public String toString();
- }
- public class LambdaVarDemo {
- public static void main(String[] args){
- int i = 7;
- IFuncInt funcInt = (num1, num2) -> {
- i = num1 + num2;
- return i;
- };
- }
- }
- public class LambdaScopeTest {
- public int x = 0;
- class FirstLevel {
- public int x = 1;
- void methodInFirstLevel(int x) {
- // The following statement causes the compiler to generate
- // the error "local variables referenced from a lambda expression
- // must be final or effectively final" in statement A:
- //
- // x = 99;
- }
- }
- }
- String str = ""; //<-- not accesible from anonymous classes implementation
- final String strFin = ""; //<-- accesible
- button.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- String ann = str; // <---- error, must be final (IDE's gives the hint);
- String ann = strFin; // <---- legal;
- String str = "legal statement on java 7,"
- +"Java 8 doesn't allow this, it thinks that I'm trying to use the str declared before the anonymous impl.";
- //we are forced to use another name than str
- }
- );
Add Comment
Please, Sign In to add comment