CHILDREN’S ACADEMY
BANI PARK
Computer Application
CLASS 10 sc/comm.
Functions in java
A function is a piece of code that is called by name.
It can be passed data to operate on (i.e. the
parameters) and can optionally return data (the
return value). All data that is passed to a function is
explicitly passed.
A method is a piece of code that is
called by a name that is associated
with an object.
To make it easy to understand we
can that
A FUNCTION IS A BLOCK OF CODE
SAVED WITH A NAME IN MEMORY AND
COMPILED AT A DIFFERENT MEMORY
LOCATION OTHER THAN THE CLASS.
Functions are used for re-usability of
code in java.
Once a function is created it can be used
any number of time in any class wherever
the same functionality is required by
following a proper mechanism.
While we define a function in java we should
know the following:
ACCESS SPECIFIERS
Java Access Specifiers/Modifiers (also
known as Visibility Specifiers )
regulate access to classes, fields and
methods in Java.
These Specifiers determine whether a field
or method in a class, can be used or
invoked by another method in another class
or sub-class.
There are four access
specifiers/modifiers in java:
1. Private: The access level of a private
modifier is only within the class. It cannot
be accessed from outside the class.
2. Default: The access level of a default
modifier is only within the package. It
cannot be accessed from outside the
package. If you do not specify any access
level, it will be the default.
3. Protected: The access level of a
protected modifier is within the package
and outside the package through child
class. If you do not make the child class,
it cannot be accessed from outside the
package.
4. Public: The access level of a public
modifier is everywhere. It can be accessed
from within the class, outside the class,
within the package and outside the
package.