• Post Reply Bookmark Topic Watch Topic
  • New Topic

accessing a static variable froma different class  RSS feed

 
vedant basu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm kind of new to this topic, so please bear with me.
Ok, i have two classes within the same program
1-project
2-called
the program's too large to post, so i'll post the relevant section


now when i try to compile, i get an illegal start of type exception at the place i've commented. i dunno if its something to do with the static variable, beacause when i try to access project.index, it works perfectly.
Any ideas? its part of a larger program, but resolving this would make the whole thing more efficient.
thanks
 
vedant basu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, i forgot to mention, both index and option are static variables in class project.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely you get the compiler error on line 10? That is where you had = instead of ==, which is the error I could see. You are also writing statements inside the class, which ought to be inside a method.
Please use code tags (not size); I have changed them and you can see how much better it looks. It would have looked even better with indentation. Correct indentation would have made the error about inside a method obvious to you.

And welcome to the Ranch
 
vedant basu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
uh yeah, i'm sorry, that was something i missed, and thanks for the update on the code tag.
but no, i can't get that if statement to work. how exactly do you access a static variable, because variable 'index' works perfectly!
thanks
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vedant basu wrote:uh yeah, i'm sorry, that was something i missed,

Was that referring to the '=' instead of '==' or
Campbell Ritchie wrote:You are also writing statements inside the class, which ought to be inside a method.
 
vedant basu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was talking about the'='.
so the problem is the 'if' statement? it should be in a 'main' method?
 
vedant basu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a main function won't let me access variable 'c' in other methods, will it?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to revise methods and classes. No, it ought not to be in the main method. That should have one statement, something along these lines:That if statement ought to be in another method. I don’t know what it ought to be called.
Also, return is a keyword, so must not be used as the name of a method, so the compiler won’t like return();
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!