I am coding, and I get stuck on a void when choosing eclipse. This happens to me a lot. Then, I cannot use the void with either the other codes that are not void. I know that void does not have a return value, but how do I use void either convert it or do something with it to use it with the other codes.

For example, something like this .size() will not work with void. I tested it with popup and put null there were no error lines; however, I still want to make sure and learn if there is supposed to be a way or not? I really would like to learn this.

Here is an example.

import java.util.*;

public class CheckVoid {

    public static void main(String[] args) {

        List<Integer> list = new ArrayList<Integer>();

        // Populating List 
        list.add(1); 
        list.add(2); 
        list.add(3); 
        list.add(4); 
        list.add(5); 

        // these give error although I saw a popup void 
        // did not give error but I put it null
        int size1 = list.notify().size();
        int size2 = list.wait(1).size();
        int size3 = list.add(1, 2).size();

    }

}

1 Answers

1
Dave G On Best Solutions

So in short, you have a fundamental understanding issue with the Java language.

Your code:

import java.util.*;

public class CheckVoid {

    public static void main(String[] args) {

        List<Integer> list = new ArrayList<Integer>();

        // Populating List 
        list.add(1); 
        list.add(2); 
        list.add(3); 
        list.add(4); 
        list.add(5); 

        // these give error although I saw a popup void 
        // did not give error but I put it null
        int size1 = list.notify().size();
        int size2 = list.wait(1).size();
        int size3 = list.add(1, 2).size();

    }

}

The above won't work for the following item:

  • list.notify() is a void method. Meaning you cannot chain method calls subsequent to that call. You would have to write list.notify(); int size1 = list.size();
  • list.wait(1) is a void method. Meaning again, as above, you cannot chain method calls subsequent to that call. You would have to write list.wait(1); int size2 = list.size();
  • list.add(int, element) is a void method. See previous explanations. You would have to write list.add(1,2); int size3 = list.size();

I hope that explanation helps.