aspose file tools*
The moose likes Beginning Java and the fly likes Problem with the for loop count variable - compiler expects a class! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with the for loop count variable - compiler expects a class!" Watch "Problem with the for loop count variable - compiler expects a class!" New topic
Author

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

Mohammed Azeem
Greenhorn

Joined: Aug 17, 2012
Posts: 12

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

Joined: Oct 24, 2010
Posts: 563
The compiler's doing it's best within its limited vocabulary to tell you that the for loop should be inside a method.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

You can't write statements outside methods. Put the for loop inside constructor or another method and it should work.


The quieter you are, the more you are able to hear.
Mohammed Azeem
Greenhorn

Joined: Aug 17, 2012
Posts: 12
Damn.... Of course.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18997
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

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


We can write statements outside method, but not all.


OCPJP 6 86%
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Besides fields declaration or definition (initialization), I can't remember of a statement that can stand outside method or initializer block.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

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.
 
 
subject: Problem with the for loop count variable - compiler expects a class!