anonymous-inner-class

Java - changing the value of a final variable from within a lambda

蹲街弑〆低调 提交于 2020-12-25 00:08:59
问题 In Java I have the following code List<Integer> myList = new ArrayList<>(); for (int i=0;i<9;i++) { myList.add(i); } Integer sum = 0; myList.forEach(i -> { sum = sum + i; // does not compile, sum needs to be final or effectively final }); for(int i : myList) { sum = sum + i; //runs without problems } My question is, why is it exactly that I cannot change the value of sum from within the lambda? It does the exact same thing as the for loop down below, or am I wrong? Interesting is also the

Java - changing the value of a final variable from within a lambda

拥有回忆 提交于 2020-12-25 00:04:31
问题 In Java I have the following code List<Integer> myList = new ArrayList<>(); for (int i=0;i<9;i++) { myList.add(i); } Integer sum = 0; myList.forEach(i -> { sum = sum + i; // does not compile, sum needs to be final or effectively final }); for(int i : myList) { sum = sum + i; //runs without problems } My question is, why is it exactly that I cannot change the value of sum from within the lambda? It does the exact same thing as the for loop down below, or am I wrong? Interesting is also the

Java - changing the value of a final variable from within a lambda

帅比萌擦擦* 提交于 2020-12-25 00:03:40
问题 In Java I have the following code List<Integer> myList = new ArrayList<>(); for (int i=0;i<9;i++) { myList.add(i); } Integer sum = 0; myList.forEach(i -> { sum = sum + i; // does not compile, sum needs to be final or effectively final }); for(int i : myList) { sum = sum + i; //runs without problems } My question is, why is it exactly that I cannot change the value of sum from within the lambda? It does the exact same thing as the for loop down below, or am I wrong? Interesting is also the

How to return the value from inner class to outer class?

假装没事ソ 提交于 2020-01-23 13:21:24
问题 What I want to do is when onSuccess method executed, the queryLogin return true ,while if onFailuer method executed , the queryLogin return false ; But as you know, in java, I cannot modify an outer class value from the inner class. So I just wonder how can I solve the problem and achieve my aim. public static boolean queryLogin(String username, String passowrd){ boolean isSuccess = false; params.put("username", username); params.put("password", passowrd); VStarRestClient.post(LOGIN_URL,

How can I access private class members of container class within the anonymouse inner class?

冷暖自知 提交于 2020-01-04 09:03:14
问题 How can I access all the member field of the class which contains the function initTimer() from within the AbstractActionClass? Thanks private void initTimer() { Action updateClockAction = new AbstractAction() { public void actionPerformed(ActionEvent e){ JLabel secLabel = m_GameApplet.GetJpanelStartNetGame().GetJlabelSeconds(); secLabel.setFont(new java.awt.Font("Lucida Handwriting", 1, 36)); secLabel.setForeground(Color.red); secLabel.setText(Integer.toString(m_TimerSeconds)); if(m

How can I access private class members of container class within the anonymouse inner class?

喜夏-厌秋 提交于 2020-01-04 09:02:08
问题 How can I access all the member field of the class which contains the function initTimer() from within the AbstractActionClass? Thanks private void initTimer() { Action updateClockAction = new AbstractAction() { public void actionPerformed(ActionEvent e){ JLabel secLabel = m_GameApplet.GetJpanelStartNetGame().GetJlabelSeconds(); secLabel.setFont(new java.awt.Font("Lucida Handwriting", 1, 36)); secLabel.setForeground(Color.red); secLabel.setText(Integer.toString(m_TimerSeconds)); if(m

Groovy - closures vs methods - the difference

流过昼夜 提交于 2019-12-21 03:52:51
问题 If you look very carefully at the picture included, you will notice that you can refactor Groovy code using the Eclipse IDE and convert a method to a closure and vice versa. So, what exactly is a closure again and how is it different than a method? Can someone give a good example of using a closure as well as why it's a useful feature? Anonymous inner classes weren't good enough? 回答1: Closure is a Closure class instance , that implements Call logic. It may be passed as argument or assigned to

Compiler is creating extra class files with $ in them

冷暖自知 提交于 2019-12-20 05:21:14
问题 I'm using Eclipse and I've written a Java application using SWT. When Eclipse compiles my program, it renames my main file into 4 different files like this: MainFile.class MainFile$1.class MainFile$2.class MainFile$3.class When I go to run this program from command line, I get Could not find the main class: MainFile.class. Program will exit. I really don't understand why this is happening. 回答1: The $ classes are for anonymous inner classes and perfectly normal. Could we see the command line

How can I access enclosing class instance variables from inside the anonymous class?

a 夏天 提交于 2019-12-20 03:21:46
问题 How do I access instance variables from inside the anonymous class's method ? class Tester extends JFrame { private JButton button; private JLabel label; //..some more public Tester() { function(); // CALL FUNCTION } public void function() { Runnable r = new Runnable() { @Override public void run() { // How do I access button and label from here ? } }; new Thread(r).start(); } } 回答1: How do I access instance variables from inside the anonymous class's method ? You simply access them if need

How can I access enclosing class instance variables from inside the anonymous class?

谁说我不能喝 提交于 2019-12-20 03:21:38
问题 How do I access instance variables from inside the anonymous class's method ? class Tester extends JFrame { private JButton button; private JLabel label; //..some more public Tester() { function(); // CALL FUNCTION } public void function() { Runnable r = new Runnable() { @Override public void run() { // How do I access button and label from here ? } }; new Thread(r).start(); } } 回答1: How do I access instance variables from inside the anonymous class's method ? You simply access them if need