• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with static variables and methods?  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to write a code that will add an exercise to a list and display it every time a new exercise event is input. The list has to be a private static data member which checks to see if the type has appeared whenever a new Exercise object is created, and if not, adds it to the list. A class method "getTypes" is used to find out what types of exercise events have been created so far. this should return a string that begins with "Exercise types: ", followed by the actual list. I don't know how to do it without using non-static variables in the static method. Any ideas?

My code so far:

 
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how to do it without using non-static variables in the static method.

But you are using a private static field ie alltypes. Can you explain in more details what your problem is?

Note:
You can't have 2 constructors with the same parameter declarations.
Wouldn't it be better to use a List rather than a String to hold a list of types?

Hint: In your constructor you should add the exercise type to the static list.
 
Emily Smith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We haven't learned how to use lists and my instructor told us to use either String or StringBuffer. allTypes is supposed to be a static class data member. I want to be able to change it every time the program runs and add on a new exerciseType to the end of the list. However, when I do that with the program as it is, I get an error message saying, "non-static variable exerciseType cannot be used in a static context". Is there some other way I can change allTypes?
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emily Smith wrote:We haven't learned how to use lists and my instructor told us to use either String or StringBuffer.

That doesn't really have anything to do with what Tony said.

To answer your initial question, I suspect we'll need a bit more of your code (as opposed to any code you may have been given for for the exercise - although that might be useful too).

If that comes to more than a hundred lines or so, I suggest you pare it down to the stuff that makes your point or asks your question.

I'm sorry if I sound clinical, but it really is very difficult to answer vague questions - and I'm afraid yours is.

Winston
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error is because you are trying to use exerciseType which is an instance variable in the getTypes method which is static.
Think about what that line does and ask yourself what a method called getTypes should do and would it even need to know what this objects type is.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!