Methods are functions that live within a class and may be accessible through the class or its instances, depending on the kind of method. Invoking a method means to execute its body statements, passing in any required parameter variables and possibly getting a value in return. A method invocation is an expression that results in a value. The value’s type is the return type of the method:
System.out.println( "Hello, World..." );
int myLength = myString.length();
Here, we invoked the methods println() and length() on different objects. The
length() method returned an integer value; the return type of println() is void (no
value).
This is all pretty simple, but we’ll see that it gets a little more complex when
there are methods with the same name but different parameter types in the same class
or when a method is redefined in a child class.
0 comments:
Post a Comment