• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static or Non Static (that is the question).  RSS feed

 
Owen Martin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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.




 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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...
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!