| ||||||||||
Yup! ("Instance" not "stance.") Think about what's happening when we say pixel.setRed() in the main method of class MyPicture. That's one class (MyPicture) accessing the method setRed of another class. That only works because setRed is declared to be public. Mark Guzdial |
You can only use a method on instances of the class where it's defined or instances of subclasses of that class. For example, Turtle instances understand turn, because turn is defined by SimpleTurtle. Strings are completely unrelated to Turtles, so you can't say "abc".turn(90). Mark Guzdial |
#4: This isn't quite true. A static method is one that can be called without needing an instance of a class. Here's an example: |
public class Greeter {
public void sayHello() {
System.out.println("Hello world");
}
public static void sayGoodbye() {
System.out.println("Goodbye world");
}
}
public class Example {
public static void main (String args) {
Greeter myGreeter = new Greeter();
//I need an instance of Greeter in order to call sayHello
myGreeter.sayHello();
//I don't need an instance of Greeter because sayGoodbye is static
Greeter.sayGoodbye();
}
}
On E. – I was actually looking for a bit of an explanation of what null is. Mark Guzdial |
Is that last part important? That it can only operate on class variables? What does that mean? You still don't explain why you'd ever want one. What class methods have we used? Mark Guzdial |
The first reason is an excellent one! Hints: Think about FileChooser and SongPhrase. Mark Guzdial |
Picture.java has a bunch of static methods in it that are there as examples. (See http://coweb.cc.gatech.edu/mediaComp-plan/101) Those aren't good examples of static methods. Better are the static methods that we use everyday. Mark Guzdial |
That is an excellent example of a static method. "null is used not to attribute a value"? What does that mean? Mark Guzdial |
Absolutely! null means "no value." But it isn't "undefined." Think about our linked lists. We set up next to point to null. That means "This node doesn't have a next, yes." REMOVEDterally, "The next here has no value." Mark Guzdial |