Calling Methods Without Parameters
Overview: 2.3.0
- Methods are a set of instructions that define behaviors for a class
- To call a method, use the dot operator
turguggins.forward()
callsturguggins
’sforward
method
Object methods
ornon-static methods
must be called on an object, not a class- Work on
attributes
of the object
- Work on
- method calls need parenthesis
- used to give parameters
- you still need parenthesis if you don’t have parameters
Procedural Abstraction: 2.3.1
procedural abstraction
allows a programmer to utilize a method without knowing how it works- the brakes in your car work even if you don’t know how
-
Use a
method signature/header
to figure out what methods do - Methods inside the same class can call each other using
methodName()
- otherwise you need to create an instance of the class, then call the method
Null Pointer Exceptions
- You must initialize an object before you can call it’s methods
- declaring an object reference without setting it to a new object will make the value
null
- If you call a method on a
null
object, you get aNullPointerException
pointer
is another name for a reference
Summary: 2.3.3
methods
are a set of instructions that define behaviors of objects in the classdot notation
is used to execute an object’s methodobject.method()
method signature
is the name followed by a parameter list- parameter list consists of the type and name of each parameter
procedural abstraction
is the concept where you can use a procedure without knowing exactly what each piece of the procedure does- a
method
orconstructor
call interrupts the sequential flow of statements, as the program will jump to execute the statements included in themethod
before continuing NullPointerException
happens when you try to use an object reference which is null- This happens when the object has not been initialized yet
object method
ornon-static method
is a method which must be called on an object of the class- utilizes an object’s specific attributes
static method
orclass method
can be called without an instance of a class
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
This was adapted from the CS Awesome curriculum, which was created by
Barbara Ericson, Beryl Hoffman, and many other CS Awesome contributors. All rights reserved.
CS Awesome is licensed under CC BY-NC-SA 4.0.