Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"Static reference to non-static method"

 
Claire Saunders
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

My current assignment is to modify a previous assignment for reading and partially solving Sudokus. We're supposed to move the display function of the original file into a new class, SudokuConsoleObserver, and implement the Observer design pattern in order to solve it (among other things I haven't gotten to yet...)

I've created SudokuObservable, ObserverApplication, and SudokuConsoleObserver, and SudokuObservable requires a notifyObservers method. The problem is, this method can't be static, and I'm not allowed to reference it in a static method... so if I try to call notifyObservers in the method I want, I get the error "Cannot make a static reference to the non-static method notifyObservers() from the type SudokuObservable." I also tried calling notifyObservers from ObserverApplication and got the same error. If I change notifyObservers to static, I get the message "This static method cannot hide the instance method from Observable."

How do I fix this? I can't add static and I can't take a static away. I suspect that I'm just misunderstanding something important about implementing Observer, since I've never done that before.

Thank you so much for your help!

Relevant code:

 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is : why are you using all these static variables ? Can't you make them non static instead ?
 
Esther Chuah
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
xD You're such a silly.

Alright. Static is used for when programs are just starting up, and don't have anything at all yet. So they've got to have a starting point, right? That's what static methods are for. They're things that already exist and are loaded in, instead of being created, like most Java objects are. (If you know anything about hardware you can compare it to a boot loader.)

You're trying to fix an error that's caused by this misunderstanding of static.

What the main problem here is is that you're trying to access a class from your static main method. That won't work. Remember, your static method exists within the program's memory. Nothing else does unless you declare it static OR create it.

That's why if you try to call a (non-existant) method in an (as yet non-existent) class, it throws a hissy fit and forces you to change it to static, (since I know you're using Eclipse).

You're doing this when you call:



You're basically calling a -class- (which is like an architectural drawing of a building) and trying to access a room from the building. What you want to do is to create objects of all your classes, which calls the workmen in and builds the building. THEN you can access the room.

Eclipse, IMHO, doesn't do so well when you ask it to solve this for you (which I assume you did). Changing everything to static will make it run, but it also means it just loads everything into your program's memory. And that just fails.

Thus:



And change everything back from static. You only want your main method (or whatever you might be using at startup) to be static.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic