Computer Science Canada Static Methods |
| Author: | CooKieLord [ Fri Nov 16, 2007 4:15 pm ] | ||
| Post subject: | Static Methods | ||
Quote: 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. Quote: non-static method constraints(int[][]) cannot be referenced from a static context
constraints = SudokuLib.constraints(sudoku);
|
|||
| Author: | rdrake [ Fri Nov 16, 2007 4:47 pm ] |
| Post subject: | 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. |
|
| Author: | CooKieLord [ Fri Nov 16, 2007 4:55 pm ] | ||
| Post subject: | 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.
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. |
|||
| Author: | rdrake [ Fri Nov 16, 2007 7:25 pm ] | ||||
| Post subject: | Re: RE:Static Methods | ||||
CooKieLord @ Fri Nov 16, 2007 4:55 pm wrote: 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.
CooKieLord @ Fri Nov 16, 2007 4:55 pm wrote: 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).
CooKieLord @ Fri Nov 16, 2007 4:55 pm wrote: 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.
[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] 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?
|
|||||