• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to point a local variable of another class in an array

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys, i wrote a code who creates a new city if there is place available inside an array.
now i want to enter one of the values of the City class to determine which this.value is the highest among the array inputs:



but I don't know how to point to a variable in other class inside the array , i tried doing:


but it says " can't find variable number"

do you have a solution?

thank you
 
rian bron
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
+ why does the method returns null?
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect the problem is here:
Here you are creating a new variable "number" that is of type City.  I suspect what you want is to put the new City into the array ci, right?  To do this follow this pattern: ci[index] = value; In this case, i is the index and new City(... is the value.
 
rian bron
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:I suspect the problem is here:
Here you are creating a new variable "number" that is of type City.  I suspect what you want is to put the new City into the array ci, right?  To do this follow this pattern: ci[index] = value; In this case, i is the index and new City(... is the value.



yeah i figure why this return null , right after i posted the post.

but still , i need to find an answer to how to point to a City variable inside the array?

lets say i want to initialize a variable called max_resident as int to be equals to the numOfResidents in the first City object inside the array.
how can i do that?

Thank you
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That depends on how the City class is written. Is numOfResidents a public variable (hopefully not), or is it a private variable with a public getter method, such as getNumOfResidents() ?
 
rian bron
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Kleinschmidt wrote:That depends on how the City class is written. Is numOfResidents a public variable (hopefully not), or is it a private variable with a public getter method, such as getNumOfResidents() ?



i didn't want to write a long code so i set this variable as  public and not private.

but can you show me the way to approach the variable who set as public variable
and i will try to show you later how to approach the same variable that i will set to private ?

thank you
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you want to get the value of a public variable of an object?

1. Get a reference to the object and assign it to a variable:



2. Use that reference to get the value of the public variable:



This is assuming your public variable is called "numOfResidents" -- you still didn't show us your City class.
 
rian bron
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:So you want to get the value of a public variable of an object?

1. Get a reference to the object and assign it to a variable:



2. Use that reference to get the value of the public variable:



This is assuming your public variable is called "numOfResidents" -- you still didn't show us your City class.



I think you didn't understand me ,
i know how to point a local variable from the class itself , i don't know how to point a local variable from a class in an array.

i can't say City city because my City object doesn't have a name because it is created inside a loop.
i can only reach it by saying ci[0], ci[1],ci[2]...

and i'm asking how can i reach the local variable from the City class *inside* the array ?

the variable in the City class is set to private
and there is  a getter method in the City class that returns value of this variable

I tried logically to say :

but i think its wrong


and the city class isn't that important , there is more than 300 lines of code over there and it's really long so i don't want to share it ..
but it consists a private  variable called numOfResidents inside the public class , and a constructor of this class which initialize this variable  
 
rian bron
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rian bron wrote:

Paul Clapham wrote:So you want to get the value of a public variable of an object?

1. Get a reference to the object and assign it to a variable:



2. Use that reference to get the value of the public variable:



This is assuming your public variable is called "numOfResidents" -- you still didn't show us your City class.



I think you didn't understand me ,
i know how to point a local variable from the class itself , i don't know how to point a local variable from a class in an array.

i can't say City city because my City object doesn't have a name because it is created inside a loop.
i can only reach it by saying ci[0], ci[1],ci[2]...

and i'm asking how can i reach the local variable from the City class *inside* the array ?

the variable in the City class is set to private
and there is  a getter method in the City class that returns value of this variable

I tried logically to say :

but i think its wrong


and the city class isn't that important , there is more than 300 lines of code over there and it's really long so i don't want to share it ..
but it consists a private  variable called numOfResidents inside the public class , and a constructor of this class which initialize this variable  



I think i figured it out, i wrote :


and i get no compile error so i think  this should work.

how to i point at a variable of an object from  the array that is set to public and not private ?
Staff note (Knute Snortum) :

Please don't quote all of the previous post, only the amount needed for context, which here is nothing.

 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

how to i point at a variable of an object from  the array that is set to public and not private ?


What have you tired?  Look to what you've already learned.
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rian bron wrote:I think i figured it out, i wrote :


and i get no compile error so i think  this should work.

how to i point at a variable of an object from  the array that is set to public and not private ?



Yes, that's what I told you. Get a reference to the object -- for some reason you seem to think that it's controversial for an array element to be an object reference, but as you see that's a perfectly ordinary thing. Then call the method, or access the variable if it's public.

I already told you how to access a public variable of an object, too. You should be able to figure out how to write that if the reference to the object happens to be an array element.
reply
    Bookmark Topic Watch Topic
  • New Topic