Professional Documents
Culture Documents
Java Methods: #Javatheeasyway
Java Methods: #Javatheeasyway
Java Methods: #Javatheeasyway
#javatheeasyway
Learning java made easy.
/chauhansumitdev
Must Know
Java is a language containing predefined keywords.
Keywords are those words which are reserved by
the java language. This means that these cannot be
used as an identifier for any variable name, object
Identifier is a unique name given to name or class name.
any entity of a program (names of
variables, classes, objects).
examples:
class UsingIdentifier
{
int uniqueName = 20;
}
abstract if
assert implements
boolean import
break instanceof
byte int
case interface
catch long
char native
class new
continue package
default private
do protected
double public
else return
enum short
extends static
final strictfp
finally super
float switch
for synchronized
null this
throw
Throws
transient
try
void
volatile
while
/chauhansumitdev
class A
var 1 Class template containing
var2 instance variables and a function
which adds the values of the
func()
{
variables.
function to add a+ b
}
object 1 object 3
2 //var1
object 2 10 //var1
3 //var2
15 //var2
5 //var1
5 //called
6 //var2 25 //called
func()
func()
11 //called
func()
Objects
/chauhansumitdev
Categories Of Methods:
Predefined Methods Use Defined Methods
These methods are already defined in These are custom methods which are
the java class. created for specific operations as
Example: Java Math class contains a required and are not already defined
method sqrt(), which calculates the in the java library.
square root of the value. Example: There may be requirement
of a function which calculates the
final salary of an employ after all the
deduction of taxes and addition of
perks.
Syntax Of A Method
A function contains a group of keywords which are added as required, but the return type, name of the method
along with a pair of parenthesis is compulsory. There are further two types of methods i.e. Parameterized and
Non-parameterized methods.
/chauhansumitdev
access_specifiers define Specifies whether the
the visibility of the the method is static or
method to other parts of non static.
the program, classes and Static methods do not
packages i.e. to which require their object of
return_type defines the pair of parenthesis may
parts of the program the the class to which they
type of value returned or may not contain
method is accessible. belong to be created in
after the execution of parameter(s) which
More on access order to access them.
the method accept argument(s)
specifiers is given
when called.
below
default
public
private
protected
/chauhansumitdev
public access:
package package
class
this has a
public
access
protected access:
package
superclass
this has a
protected
access
inherited
subclass
inheritance is the process by which a new class is created which inherits the features from its
parent class called superclass.
/chauhansumitdev
private access: package
class Cylinder{
double radius;
double height;
void calcvolume()
{
double volume= 3.14*radius*radius*height;
System.out.println("The volume of cylinder is "+ volume);
}
}
/chauhansumitdev
class CylVolCalc
{
public static void main(String[] args)
{
Cylinder A=new Cylinder();
A.radius=10;
A.height=100;
A.calcvolume();
}
Process:
A very important point to note is that both the classes here are created in the same folder and
class CylVolCalc is executed. All of the execution begins from the class containing the main
method. As the main method gets executed an object of Cylinder class is created and values for
radius and height are assigned using .(dot operator). A.calcvolume() calls the calcvolume() method
created in object A, this in turn calculates the volume of the cylinder and stores in volume variable.
The value stored is displayed on the screen using println() method.
Output:
The volume of cylinder is 31400.0
class CylVolCalc
{
public static void main(String[] args)
{
Cylinder B=new Cylinder();
B.radius=5;
B.height=50;
Output:
B.calcvolume(); The volume of cylinder is 3925.0
The volume of cylinder is 847.8
Cylinder C=new Cylinder(); The volume of cylinder is 10770.2
C.radius=3;
C.height=30;
C.calcvolume();
/chauhansumitdev
Happy Learning
/chauhansumitdev