• Post Reply Bookmark Topic Watch Topic
  • New Topic

reuse in another class a variable declared in the main method  RSS feed

 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, this is probably a basic question but I have some issue with the following classe : I would like to re-use the variable field2d which is an array of char (declared in the main method of the class Project) in another class.
For example, I would like to iterate over this array but I got the following error : « field cannot be resolved to a variable » in the for loop. What I have overlooked? thanks!




 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank Poupard wrote:Hello, this is probably a basic question but I have some issue with the following classe : I would like to re-use the variable field2d which is an array of char (declared in the main method of the class Project) in another class.
For example, I would like to iterate over this array but I got the following error : « field cannot be resolved to a variable » in the for loop. What I have overlooked? thanks!


Directly? Well, you can't. Local variables are ... well ... local to the method where they are declared. However, you have a few options as alternatives... One. You can use a different type of variable. One with a scope that is accessible by both methods, such as instance or static variables. Two. You can also pass the instances around, such as using method parameters and/or returns. This second case will declare different local variables, but the different references can be made to point to the same instances.

Henry
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would make the local variable a field and then create a getter for it. Then you could call the getter with an instance of your Project class.

Right now a lot of your code is in main() so your field would have to be static. But in the real world, you wouldn't have more than two or three lines in main(), so your field could be an instance field as it should be.

Try this out in code and post it here. If you have problems, post what you tried.
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks both of you. So, I have made the following changes but still get some errors. I get now the error in the same test method : "Cannot make a static reference to the non-static field
field2dim". Don't really know what to do !




 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank Poupard wrote: I get now the error in the same test method : "Cannot make a static reference to the non-static field
field2dim". Don't really know what to do !


Do you know the difference between a static variable and an instance variable? And the difference between a static and non-static context?

Basically, to access an instance variable, you actually need to specify an instance. And from how you are using it, static method with no reference used, there isn't an instance specified.

Henry
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I realized I should have written


instead of

But still, I get the error, "Type mismatch: cannot convert from int to boolean"

I think I make the difference between static and non static context (I guess we are here in non static context because no object is involved here, only the Class Project as a whole) but something probably escapes me...
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A static reference (variable or method) is made with the class name, like this:

But you "don't want to do that." What you want to do is get the code out of the static main() method and into an instance (nonstatic) method. Then define an instance field (nonstatic variable) and make it private. Then make your getter nonstatic.

This may seem like a lot of work for "nothing", but it's good to get use to coding like that in the future. It's very rare to have more than a few lines in main() in real projects.
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it really
char[][] terr2d = Projet.getTerrain2d();
and not
char[][] terr2d = Project.getTerrain2d();
?
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it really
char[][] terr2d = Projet.getTerrain2d();
and not
char[][] terr2d = Project.getTerrain2d();
?

Sorry, I messed up with languages. There were mistakes also in the for loop, which anyway was only here for the sake of testing. I made it worked finally ! Everything is clear now, thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!