Size has private access in ArrayList

I wrote a pretty standard bit of code using a String filled with an ArrayList , but when I try to run it, I get the following error:

error: size has private access in ArrayList .

The code is as follows:

 System.out.println(testedArticles.size); 
+6
source share
2 answers

You are trying to access a private member of an ArrayList , parts of its internal work that should not be used externally

If you want to get the size of the arraylist, you want to use the method:

 arraylist.size() 

Why does it look like this

This gives the ArrayList class the ability to store the size in whatever way it wants. It just returns size , perhaps, but instead, it could do a few other things. For example, he could calculate the size lazily, in which he calculates only if someone asked him to, then he keeps this value until it becomes invalid (as more objects are added). This would be useful if calculating the size was expensive (it is very unlikely that it would take place here), it often changed and was called only occasionally.

+10
source

There is nothing like ArrayList.size. You need to use the .size() method.

You need to use

 System.out.println(testedArticles.size()); 

instead

 System.out.println(testedArticles.size); 
+3
source

Source: https://habr.com/ru/post/956243/


All Articles