-2

I recently installed Java 10 and upgraded my Maven project to Java 10 then. I have a block of code where I'm trying to loop over a list of objects(in this case TableModelListeners from Swing's JTable system). This code is reported as an error though:

for(TableModelListener objListener : getTableModelListeners())
        objListener...

It's claiming in the 2nd line that objListener isn't declared or instantiated. It works when I convert it to a regular for loop like such:

for(int iLength = getTableModelListeners().size(), iLoop = 0; iLoop < iLength; iLoop++)
      {
      TableModelListener objListener = getTableModelListeners().get(iLOop);
      //...
      }

But I don't want that as it's more code that is somewhat unnecesary. What am I doing wrong here if Java 10 somehow changed the format of the for each loop?

Edit: As requested - Here is the code to getTableListeners - It's just lazy creation:

protected List<TableModelListener> getTableModelListeners()
{
    if(mLstTableModelListeners == null)
        mLstTableModelListeners = new ArrayList<TableModelListener>();

    return(mLstTableModelListeners);
}
Naman
  • 27,789
  • 26
  • 218
  • 353
Seth D. Fulmer
  • 490
  • 1
  • 4
  • 21
  • 1
    please add to you question the method for `getTableModelListeners` – petey Sep 27 '18 at 14:05
  • 1
    [1] How are you compiling the code? javac? Maven? Within some IDE? [2] As petey said, you need to show more of your code [3] Show the exact compilation error (maybe using a screen shot). – skomisa Sep 27 '18 at 17:41
  • I've added getTableListeners - It's just lazy creation - if the list object is null, create it but return it after that either way. As for how I'm compiling the code - I'm in Eclipse but it's a Maven project. I don't get an 'error' - just that objListener is underlined and Eclipse says it's undeclared. – Seth D. Fulmer Sep 27 '18 at 18:41
  • The solution I have since used since I posted this was to use collection streams which were a feature since Java 8 and which I've never used until now - A feature with which I'm not totally comfortable, but it does its job now - I just would have preferred to include everything in the same method and not have an anonymous Consumer class in there too. – Seth D. Fulmer Sep 27 '18 at 18:45
  • I didn't because it wasn't complete. All I could put was objListener.(and that's the end) - Eclipse wouldn't autocomplete or provide suggestions so I had no idea what to enter. I could only finish it with a regular for loop or using stream manipulation. Now I can't try it even because my laptop is dead as of last night and I'm trying to get access to my tower in storage by my parents to replace it - first I'll have to upgrade that machine to Java 10 but that's relatively simple. – Seth D. Fulmer Sep 29 '18 at 11:08

1 Answers1

0

No, Java 10 did not touch for-each loops. If the code compiled with Java <10, it should do the same on Java 10.

I'm sure that if you build the project with Maven, you will not see a compile error. If that's so, Eclipse has a problem here, but it is likely just a hiccup. Try the various resolution strategies like deleting the problem in the Problems View, cleaning the project, or even reimport it.

A note: The line return(mLstTableModelListeners); is unidiomatic - return mLstTableModelListeners; would be the common way to do this.

Nicolai Parlog
  • 47,972
  • 24
  • 125
  • 255
  • I add parentheses because it removes any doubt about order of operations. Sure, in my example there's no doubt anyway but let's say I was returning 5*(4+x) or even function(params) && !function(params)...what's the order of operations there? I never know nor do I want to know. I will continue to use parentheses and only remove parentheses and simplifying when things get really confusing. It does no damage or hinderance to the code – Seth D. Fulmer Sep 28 '18 at 11:43
  • The parenthesis are a tangent, but I still want to comment. I agree with using them for complicated expressions, but in `return $expression`, there is no logical alternative to evaluating the entire expression before returning (i.e. `return` **must** be the last "operation"). The unfortunate line break in my answer also masked that there should be a space after `return`. That aside, did you try my recommendation to build with project outside of Eclipse? – Nicolai Parlog Sep 29 '18 at 07:53
  • @SethD.Fulmer then, why did you write `iLoop < iLength` in your loop code, instead of `((iLoop) < (iLength))`? You code style is inconsistent. – Holger Sep 30 '18 at 11:04
  • Put simply it's how I learned for loops way back when I learned C in the 90s. And why're you quarreling over ...punctuation? That's like quarreling over the use/misuse of commas/semicolons in written text - people don't like it & it makes little to no difference in the meaning of the words. return(x), return x; - it makes NO difference which you use - but the 1st is loads easier to read especially if one doesn't know the order of operations - as I don't - Nor do I ever want to learn them - I've always used parentheses for order of operations corralling. Not always - who cares otherwise? – Seth D. Fulmer Sep 30 '18 at 17:04
  • I think everybody said their piece regarding the parenthesis, so lets stop discussing that. @SethD.Fulmer, did you try my recommendation to build the project outside of Eclipse? – Nicolai Parlog Oct 01 '18 at 11:14
  • As I said in another comment - No because at the time, the code wouldn't even auto-complete - I got as far as objListener. and it would underline it while typing and not provide suggestions. Now I can't even test it because my laptop died & with it the code as that section hadn't been put to git yet & I'm trying to reconstruct the code. When I get the code reconstructed in 1-2 days, I will try to remember what method to call since I won't be able to autocomplete it & type it in and test it with a mvn to see if the error is the same there as in Eclipse. By my experience it should be the same. – Seth D. Fulmer Oct 01 '18 at 15:15
  • 1
    The lack of auto-completion is another indicator that Eclipse has a problem here. I forgot to say that, when you're using Java 10, you have to use Eclipse's most recent version. Maybe that's the problem. – Nicolai Parlog Oct 01 '18 at 20:19