• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing ArrayList from Main method  RSS feed

 
Wendy Jackson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here I have implemented a telephoneQueue. Basically am stuck on how to access my arrayList in my public void simulation(PhoneCall element) {} method


EDIT extra information: my public void simulation(PhoneCall element) {} is basically checking if the TelephoneQueue is full then removing the first element, adding a random call to the TelephoneQ then printing it. am getting cannot find symbol for my callerDetails and rand even though they are defined in main. Any suggestions what am doing wrong..

 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any variable defined in the main() method, is a local variable and as such can only be seen in main().  Instance variables can be seen in all methods and I defined outside of any method, usually at the top of the class.  front, rear, count, and phone are all instance variables in your program.
 
Junilu Lacar
Sheriff
Posts: 11490
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wendy Jackson wrote:EDIT extra information: my public void simulation(PhoneCall element) {} is basically checking if the TelephoneQueue is full then removing the first element, adding a random call to the TelephoneQ then printing it.

Actually, if you look at the code carefully, that's not exactly what it is doing. A more accurate description is that this code checks if the current TelephoneQ is full and if it is, it will remove the first element in the queue. Otherwise, it will try to do something it can't do and then print a blank line. I believe that what you actually intended to do on line 91 was to add some random PhoneCall object to the TelephoneQ.

What you're doing wrong: This code executes in the context of a specific TelephoneQ object.  The callerDetails list is a local variable in the main() method. Trying to use the callerDetails variable in the context of an instance method is like trying to read a book that's on a shelf in your local public library (i.e. your main() method) while you're sitting in your living room (the simulation() method). It just not possible because callerDetails can only be accessed from within the static main() method.

Questions to ask yourself: Why does the simulation() method take an argument, PhoneCall element? Why are you not using this parameter at all in the simulation() method? What's the point of declaring that the simulation() method takes a PhoneCall object as its argument but then not using that object at all in the method?

 
Junilu Lacar
Sheriff
Posts: 11490
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and Welcome to the Ranch!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!