This document was written by CS 290W TA Joshua Kay and was last modified
Okay, we talked about Objects and their Properties. Now it is time to introduce Methods.If Properties describe Objects, then Methods describe what objects can do.
Let's create a frog. ---
Frog froggie=new Frog("kermit");
What can frogs do? We know that they are green, and slimy, but what do they do?
Well, frogs can swim, they can jump, they eat flies, they stick their tongues out
So, froggie.jump(); --- this represents when froggie jumps, right?
In a program - this represents when our object froggie of the frog class accesses its jump method
Let's look at some code...
class Frog { String name; Frog() { //default constructor } Frog(String name) { this.name=name; } public void jump() { System.out.println("Frog Jumping!"); } }When we say froggie.jump() - the compiler looks at froggie, determines that is a specific object created by the Frog() class, and then checks Frog()'s available methods. When it finds jump - then it performs what jump asks for.
Notice that methods, unlike properties, are followed by parentheses (). This is because methods expect arguments - if there are no arguments, or parameters, to be sent to the method, the the argument is null() - but the parentheses are still needed.
This will actually make coding a little easier for you - you will be able to distinguish methods and properties at a glance, even with programs that you didn't write.
So, properties - froggie.name=kermit;
But methods - froggie.jump();