Guest User

Untitled

a guest
Apr 24th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. PhoneNumber(String phoneNumber) {
  2. numberLength = 7; // <== assignment to numberLength
  3. String currentNumber = phoneNumber.replaceAll(
  4. regularExpression, "");
  5. if (currentNumber.length() == numberLength)
  6. formattedPhoneNumber = currentNumber;
  7. else
  8. formattedPhoneNumber = null;
  9. }
  10.  
  11. public class Foo {
  12.  
  13. public void baz(int bar) {
  14. // While the next line is commented, bar is effectively final
  15. // and while it is uncommented, the assignment means it is not
  16. // effectively final.
  17.  
  18. // bar = 2;
  19. }
  20. }
  21.  
  22. final int variable = 123;
  23.  
  24. int variable = 123;
  25. variable = 456;
  26.  
  27. int variable = 123;
  28.  
  29. final int number;
  30. number = 23;
  31.  
  32. int number;
  33. number = 34;
  34.  
  35. @FunctionalInterface
  36. interface IFuncInt {
  37. int func(int num1, int num2);
  38. public String toString();
  39. }
  40.  
  41. public class LambdaVarDemo {
  42.  
  43. public static void main(String[] args){
  44. int i = 7;
  45. IFuncInt funcInt = (num1, num2) -> {
  46. i = num1 + num2;
  47. return i;
  48. };
  49. }
  50. }
  51.  
  52. public class LambdaScopeTest {
  53. public int x = 0;
  54. class FirstLevel {
  55. public int x = 1;
  56. void methodInFirstLevel(int x) {
  57.  
  58. // The following statement causes the compiler to generate
  59. // the error "local variables referenced from a lambda expression
  60. // must be final or effectively final" in statement A:
  61. //
  62. // x = 99;
  63.  
  64. }
  65. }
  66. }
  67.  
  68. String str = ""; //<-- not accesible from anonymous classes implementation
  69. final String strFin = ""; //<-- accesible
  70. button.addActionListener(new ActionListener() {
  71. @Override
  72. public void actionPerformed(ActionEvent e) {
  73. String ann = str; // <---- error, must be final (IDE's gives the hint);
  74. String ann = strFin; // <---- legal;
  75. String str = "legal statement on java 7,"
  76. +"Java 8 doesn't allow this, it thinks that I'm trying to use the str declared before the anonymous impl.";
  77. //we are forced to use another name than str
  78. }
  79. );
Add Comment
Please, Sign In to add comment