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.
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.