| Author |
Static or Non Static (that is the question).
|
Owen Martin
Greenhorn
Joined: Mar 05, 2010
Posts: 14
|
|
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
Joined: Aug 31, 2004
Posts: 11343
|
|
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...
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
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
|
 |
 |
|
|
subject: Static or Non Static (that is the question).
|
|
|