• 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

Static or Non Static (that is the question).

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys, I am just trying out 'simple' Java techniques for practise and I am already stuck.

Don't worry about the code itself in terms of efficiency etc, I just want to know why this doesn't work.

I know I can change the 'add' method to static but I done that in a previous program and nearly all my methods were made static and running it etc started to confuse me.

I have one class, and a test class.




 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods are associated with the class. Instance (non-static) methods are associated with an instance of the class.

You cannot call an instance method using the class...

Instead, you need to create an instance of the class, and call the method using that instance...
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may be getting confused because you have strangely-named classes. You have a class named Students which appears really to be a queue. You ought to have a Student class and a HelpQueue class. Then you create a HelpQueue object, and add Students to the Queue (or remove them).

You would do better to use a class which implements the Queue<T> interface rather than a straight List<T>. ArrayDeque would be a good choice. Note there are methods named differently for Queues, eg offer and poll.

Never never say == false or == true. Apart from that being very bad style, you can get all sorts of really nasty errors if you accidentally write = instead of ==. It's notbut
 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic