This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""Static reference to non-static method"" Watch ""Static reference to non-static method"" New topic
Author

"Static reference to non-static method"

Claire Saunders
Greenhorn

Joined: Sep 29, 2009
Posts: 5
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

The question is : why are you using all these static variables ? Can't you make them non static instead ?


[My Blog]
All roads lead to JavaRanch
Esther Chuah
Greenhorn

Joined: Dec 08, 2009
Posts: 1
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: "Static reference to non-static method"
 
Similar Threads
Block Sliding Puzzle
Just wanted to share my code SudokuSolver !
help with game
Have board printed twice in second method. Board class.
June Newsletter Puzzle