• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I use a returned boolean array in another class ?  RSS feed

 
John Ironmonger
Greenhorn
Posts: 8
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

Here's what i've got so far :


Here's the class i'm using it in :


How do I use the array unit from the class UnitBools in my class UnitFrames ? I want to be able to update and check the array from other classes.

Thank you for any help.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Ironmonger wrote:
How do I use the array unit from the class UnitBools in my class UnitFrames ? I want to be able to update and check the array from other classes.


There is no "array unit from the class UnitBools". And hence, nothing that can be used elsewhere.

The UnitBools does have a method named units() that has a parameter named unit. In that case, that variable is a local variable; it can't be used outside of the method, and it only exists for the lifetime of a method call. Also, no actual array has been created in the method -- as the array reference is passed into the method. So, I guess you need to find where the method is called, see what is actually passed to the method, and if it is always the same array, you can see if the UnitFrames class has access rights to it.

Henry
 
John Ironmonger
Greenhorn
Posts: 8
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
John Ironmonger wrote:
How do I use the array unit from the class UnitBools in my class UnitFrames ? I want to be able to update and check the array from other classes.


There is no "array unit from the class UnitBools". And hence, nothing that can be used elsewhere.

The UnitBools does have a method named units() that has a parameter named unit. In that case, that variable is a local variable; it can't be used outside of the method, and it only exists for the lifetime of a method call. Also, no actual array has been created in the method -- as the array reference is passed into the method. So, I guess you need to find where the method is called, see what is actually passed to the method, and if it is always the same array, you can see if the UnitFrames class has access rights to it.

Henry


Thanks for the reply. Would there be an easier way to do this then ? I just want to create the array of booleans in the first class, and have it mutable from other classes.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Ironmonger wrote:
Thanks for the reply. Would there be an easier way to do this then ? I just want to create the array of booleans in the first class, and have it mutable from other classes.


Why not do exactly what you stated? Instantiate an array of booleans that is accessible from both classes? A possible location is the use of a public static variable -- or non-public, but with public access methods.

Henry
 
John Ironmonger
Greenhorn
Posts: 8
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
John Ironmonger wrote:
Thanks for the reply. Would there be an easier way to do this then ? I just want to create the array of booleans in the first class, and have it mutable from other classes.


Why not do exactly what you stated? Instantiate an array of booleans that is accessible from both classes? A possible location is the use of a public static variable -- or non-public, but with public access methods.

Henry


To be honest I don't know how, would you mind taking me through one of those options for my problem ?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Ironmonger wrote:
To be honest I don't know how, would you mind taking me through one of those options for my problem ?



Can you tell us just what you "don't know how" about? Is it the creating array part? or having them referred to by static variable part?

Anyway, these tutorial may help...

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html (which shows how to create arrays)
http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html (which shows how to use static variables that can be accessed by multiple classes)

Henry
 
John Ironmonger
Greenhorn
Posts: 8
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
John Ironmonger wrote:
To be honest I don't know how, would you mind taking me through one of those options for my problem ?



Can you tell us just what you "don't know how" about? Is it the creating array part? or having them referred to by static variable part?

Anyway, these tutorial may help...

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html (which shows how to create arrays)
http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html (which shows how to use static variables that can be accessed by multiple classes)

Henry


Thank you for the links, i've done some reading and i've come up with this :



But it's giving me an error when it gets to the Sysout. Any ideas?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Ironmonger wrote:
Thank you for the links, i've done some reading and i've come up with this :



But it's giving me an error when it gets to the Sysout. Any ideas?



In order to use an array reference variable, you first need to initialize the array reference variable.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!