• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Problem with the for loop count variable - compiler expects a class!

 
Ranch Hand
Posts: 97
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Good Morning,

I've stepped up from tutorials but I'm still a beginner. I am coding my own "experiments" for practice.

At line 10 in the listing below the loop count variable is "i". It is initialised in the loop and has a life only within the loop.

When I build the project, Netbeans thinks the variable i is a class and reports this error at line 10:

illegal start of expression
cannot find symbol
symbol: class i
location: class MyPanel


Can't see what I'm doing wrong. Anybody help?


By the way the structure of the project is:

* Class MyRectangle defines a rectangle object.
It has a constructor that takes an integers width and height as parameters and then calculates the co-ords of the corners with the rectangle centred at (400,400).

* The class MyPanel (the listing which is shown below) is a JPanel object on to which MyRectangle objects are drawn.

* MyPanel is then displayed in the contentpane of a JApplet.

I have succeeded in drawing one rectangle, now I want to extend the experiment by drawing three concentric rectangles - hence the "for" loop which creates three instances of the MyRectangle class.

Thanks.





 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler's doing it's best within its limited vocabulary to tell you that the for loop should be inside a method.
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't write statements outside methods. Put the for loop inside constructor or another method and it should work.
 
Mohammed Azeem
Ranch Hand
Posts: 97
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Damn.... Of course.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Greg Brannon wrote:The compiler's doing it's best within its limited vocabulary to tell you that the for loop should be inside a method.



Well, if the purpose of that code is to initialize that instance variable then a constructor or even an instance initializer would be more appropriate.

Henry
 
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kemal Sokolovic wrote:You can't write statements outside methods.



We can write statements outside method, but not all.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Besides fields declaration or definition (initialization), I can't remember of a statement that can stand outside method or initializer block.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nikhil Sagar wrote:

Kemal Sokolovic wrote:You can't write statements outside methods.



We can write statements outside method, but not all.



Yes, but not the way you're probably thinking.

All statements must be inside a method, a constructor, or an initializer block. If that's what you meant, you're correct. Statements can never go directly inside a class, where the OP put his for loop.
 
Marshal
Posts: 27450
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a distinction between statements and definitions. A definition declares a variable:



whereas a statement doesn't declare a variable:



Note that it's possible to do both at the same time:



And this is classified as a definition, not a statement. So all statements must be inside a method, a constructor, or an initializer block, but declarations can appear outside of those things.
 
I've never won anything before. Not even a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic