• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Mohammed Azeem
Ranch Hand
Posts: 49
3
  • Mark post as helpful
  • send pies
  • 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.





 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • 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.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • 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: 49
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damn.... Of course.
 
Henry Wong
author
Marshal
Pie
Posts: 21016
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
Java Python Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Clapham
Sheriff
Posts: 20980
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic