• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing private fields  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am trying to create a method for my "ticketmachine" object that counts ticket objects in an arraylist of "ticket" objects with a specified field "route". The method works if i change the field type of the ticket class to public, however according to the homework task "route" of the ticket class needs to remain a private field.


here is my code for the method.

Is my general approach to the problem wrong? also is there a way to access a private field?

sorry if this is not very coherent, I find it hard to describe this problem.

if someone could point me in the right direction i would be most grateful!
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

You would need to add a getter for the route field in the Ticket class. See http://docs.oracle.com/javaee/6/tutorial/doc/gjbbp.html -

Edit: Here's an example from the Java SE Tutorials: http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html

When you post code, UseCodeTags (←click on that link to learn how)
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may also want to use a for-each loop which is a more natural fit for your problem than a while loop.
 
Kayleigh Gilroy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That getter did the trick, thanks very much! 😊

Also I will try changing out the while for a for-each loop like you suggested.

Can't believe i was stuck on that for so long.

P.s. il be sure to make use of the code tags in future too!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!