D3.1 – 3.3 – Defining Terms

D3.1 Define the terms   – class, identifier, primitive, instance variable, parameter variable, local variable

What is a class?

class, in the context of Java, are templates that are used to create objects, and to define object data types (data members) and methods. 

What are identifiers?

A Java identifier is a name given to a package, class, interface, method, or variable. It allows a programmer to refer to the item from other places in the program.

What are Primitive Data Types?

Primitive types are the most basic data types available within the Java language.

What is a variable?

A variable provides us with named storage location for a value that a program can manipulate.

They must be declared before they can be used and can only contain data of a particular type (in Java)

All Variable names are identifiers

What are instance Variables?

Instance variables are non-static variables and are declared in a class outside any method, constructor or block.

As instance variables are declared in a class, these variables are created when an object of the class is created and destroyed when the object is destroyed.

We may use access modifiers (public/private/protected) for instance variables.

If we do not specify any access modifier then the default access modifier of the class will be used.

What are Parameter Variables?

Parameters allow us to pass information or instructions into functions and procedures.

Parameters are the names of the information that we want to use in a function or procedure.

The values passed in are called arguments.

What are local variables?

A variable defined within a block or method or constructor is called local variable.

These variable are created when the block is entered or the function is called and destroyed after exiting from the block or when the call returns from the function.

These variable are created when the block is entered or the function is called and destroyed after exiting from the block or when the call returns from the function.

This image has an empty alt attribute; its file name is Screen-Shot-2019-11-13-at-3.14.17-PM.png

D3.2 – Define the terms: method, accessor, mutator, constructor, signature, return value

What is a method?

A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method’s name.

In other languages It may be referred to as a function

A method can be described as a subprogram that acts on data and often returns a value.

Each method has its own name (identifier).

We can reuse the same piece of code over and over again

What is an Accessor (Getter)

An accessor is a type of method used in Java OO programming that which returns the value of a private instance (class) variable.

Getters are used to effectively protect your data.

What is a Mutator (Setter)

A mutator method is a method used to control changes to a encapsulated instance (class) variable/state.

What is a constructor?

A constructor method is an instance method (defined inside a class) that is invoked (called) when an object of that class is created (by using the new keyword)

Object creation rule in Java: When an object is created, one of the constructor method in the class must be invoked (to initialize the instance variables in the object)

A constructor has the same name of the Class and does not have a return type

Constructor Overloading

You can have more than one Constructor to a class by using constructor overloading.

In order to do this they must all have different parameter lists

This allows you to set different variable values dependent on the input

What is a signature?

A method signature is part of the method declaration. It is the combination of the method name and the parameter list

The reason for the emphasis on just the method name and parameter list is because of overloading methods that have the same name but accept different parameters.

Here you can see the name (Add) and the parameters (num1 & num2)

The return word

return is a reserved keyword in Java; it cannot be used as an identifier.

It is used to exit from a method, with or without a value.

For methods that define a return type, return statement must be immediately followed by return value.

The void word

The void keyword is use for methods that do not return a value

Here below the method does NOT return a value, it just prints to the console

Using return

This method requires a return type (String in this method). This will still print “Hey there” to the console but will return ”I have returned”

This will print “Hey there” to the console
This will print out “I have returned” to the console

Methods vs Procedures vs Functions

Methods are also known as Procedures or Functions:

Procedures: don’t return any value (void).

No method can return more than one value at a time in Java.

Functions are just methods!