
-----------------------------------
CooKieLord
Fri Nov 16, 2007 4:15 pm

Static Methods
-----------------------------------
Of course, it doesn't always make sense for methods to be directly attached to objects. In this case, we have "static" methods, like our "main" method. Static methods are associated with the class itself, rather than an object of that class.


Can someone explain to me more in depth what "statatic" means. I would like to understand it because I am getting an error.

non-static method constraints(int

    public static void generateConstraints(int

-----------------------------------
rdrake
Fri Nov 16, 2007 4:47 pm

RE:Static Methods
-----------------------------------
A non-static method must be invoked on an object.  A static method can be invoked without creating an object first.

In your case you should probably have a Sudoku class with each of those methods being non-static.

-----------------------------------
CooKieLord
Fri Nov 16, 2007 4:55 pm

RE:Static Methods
-----------------------------------
The professor wants us to create a Sudoku program. They did some work for us, like the graphic interface and input method. We need to work on the algorithms.

The students need to make the algorithm that checks the validity of the answer, i.e. check for duplicate entries in a row, column or 3x3 zone, etc. They did not give us access to the entire source, we only have a jew .java files with half-completed methods. Some of the files in the package is simply a .class.

As such, I cannot check to see how they call the algorithm. Is it static? Not static? Does it even matter in my case?

Here is the entire class that I'm working on, and with my current progress at the moment.
import javax.swing.JOptionPane;

public class SudokuLib {

  // Q5: TO COMPLETE
  // METHOD verifyGuess: verifies if the guess would result is a valid sudoku grid
  // GIVENS: the row, the column, and the value to insert (the guess)
  //         the sudoku grid and its constraints,
  //         a boolean value indicating if we need to display an error message
  //          (invoked with true in the program, with false in the tests)
  public static boolean verifyGuess(int row, int col, int move, int

I'd like it if you paid attention to Q5 (at the top) and Q3 (near the middle).

Meanwhile, I will dig in to see what exactly is an object. I had always thought that an object would be something more visual such as combo boxes, scroll bars, etc. I am most likely wrong. I am now inclined to think that an object can be an array, a vector, a method or a class.

Would it also be possible to expand a bit more on the subject of static methods?
[Edit]Hold that thought on static methods, I found what I might be needing in the Intro to Java tutorial. I'm reading it and trying to understand it right now.[/edit]

Thanks for your help.

-----------------------------------
rdrake
Fri Nov 16, 2007 7:25 pm

Re: RE:Static Methods
-----------------------------------
As such, I cannot check to see how they call the algorithm. Is it static? Not static? Does it even matter in my case?Chances are, given how the rest of the code is structured, it is a static method as well.

Meanwhile, I will dig in to see what exactly is an object. I had always thought that an object would be something more visual such as combo boxes, scroll bars, etc. I am most likely wrong. I am now inclined to think that an object can be an array, a vector, a method or a class.An object can be whatever you say it should be.  When you create a class with non-static methods, then create an instance of that class, you are creating an object.  The objects you create in programming are much like real life objects; where they have both behaviours and properties.  For example, a car can drive (behaviour) and is a certain colour (property).

Would it also be possible to expand a bit more on the subject of static methods?
Essentially static methods are useful when they're dealing with operations that don't need to be done on an object itself.  Things like factory methods are typically useful most as static methods, as all they do is create objects, they don't modify an object.

The Introduction to Java tutorial is an excellent way to get started.

Think of it this way, let's say you have a database application that deals with records.  Would it make more sense to have each record have its own delete() method, or to have a static delete(Record r) method?
Record r = new Record();
r.delete();vs.
Record r = new Record();
Record.delete(r);I would say the first seems more "natural."
