Chapter 3 - Classes some basics Flashcards
what is a class in terms of type
- A class is a special kind of programmer-defined type, and variables can be declared of a class type - A value of a class type is called an object or an instance of the class
primitive vs class type values
- prim -sinlge piece of data
- classes - multiple pieces of data as well as methods
- All instances of a class have the same methods
– All instances of a class have the same pieces of data (i.e.,
name, type, and number)
– For a given instances, each piece of data can hold a
different value
what are the contents of class definition?
- specifies that data items and the methods that all of its objects will have
- data items - fields - instance variables
how to invoke a method
use the calling object and the mthod name
classVar.mymethod();
two method categories
- perform action - dont return, void
- compute and return a value
keywords used for methods
typeReturned methodname
void
main
turning a method into a void method
objectName.returnedValueMethod()
local variable
variable declared within a method definition
examples of local variables
- declared in the main method
- method parameters
- If two methods each have a local variable of
the same name, they are still two entirely
different variables - for loop
formal parameters
some methods need additional data via a list of parameters in order to perform their work
what does a parameter list do
A parameter list provides a description of
the data required by a method
– It indicates the number and types of data
pieces needed, the order in which they must be
given, and the local name for these pieces as
used in the method
what are arguments
actual parameters
when a method is invoked, the appropriate values must be passed in the form of arguments
number and order must match that of the para list
type of each argument must be compatible with that of the corresponding parameter
call by value mechanism
Call by Value means calling a method with a parameter as value. Through this, the argument value is passed to the parameter.
pass by reference vs pass by value
Pass by Value: The method parameter values are copied to another variable and then the copied object is passed, that’s why it’s called pass by value.
Pass by Reference: An alias or reference to the actual parameter is passed to the method, that’s why it’s called pass by reference.
what happens if arguments and parameters dont match
java will try an automatic type conversion