Methods
Deffination : Methods are block of code used to
perform Perticular Task.
Example:
Advantage
>Code Re-useable
>Modularity (Things will become easy with
methods bcz work is divided)
>Readable
Syntax
[Access Modifier][Modifier]returnType
methodName([Formal Arg])
{
Task;
}
Exactly what is the meaning of the above Syntax.
AccessModifier--→Decide the Visibility
Ex:--→ public >protected>default>private
Modifier-→Decides Characteristic
Ex:static,abstract,final….etc.
returnType->Decides what output we get
ex:void (No Output From Method)
primitiveDatatype
Non-primitive Datatype
Formal Arg→Decides Input
[Link](int a,int b) Asking for 2 Inputs
[Link]()--It is Not asking any Input
p.s.v shikar(String x,int x )-It is asking 2 Input
WAJP to create a class which is having 2 Methods
In the Above example Only Main Method
executing bcz
……When ever we execute the Program JVM Only
Calls Main Method so Only Main Method
Executed…..
Are you trying to Say In Order to Execute any
Method we need to call?
➢Yes
How to Call the Methods.
➢Syntax
o methodName([actual arg]);
Identify Perfect MethodCall
Example Programs
What is return ?
➢return is a keyword helps to exit from Method
or terminate the Method.
➢We need to use return keyword inside the
method
➢Use return stmt as the last stmt else we get
Unreachble stmt error.
Note:When ever we are having method
If a programmer forgets to add return
keyword last then compiler will add at
compile time.
What is returnType?
➢It is a component of Method
➢It helps to understand what type of output
given by the method at the time of exit
Note:void method will not give any output
Stack Explaination (fig-0.1):
WAJP to Find the Factorial
WAJP to Check for the Leap Year
Static
➢It is a keyword as well as a Modifier
➢The resource which is same for all the Objects
is should be static
Example
School------Student1,Student2,Student3….
Board-------Student1,Student2….
PrimeMinister---Person1,Person3…….
Lang-----Person1,Person2,Person3….
Kitchen-----Father,Mother,Brother,Sister,You
Sun------Earth,Jupiter,[Link]
➢We are having following static Members
o Static Variable
o Static Method
o Static Initializers
▪ Single Line static Initializer
▪ Multi-Line static Initializer
Static variable:
➢Any variable Declared or Declared and
Initialized in Global Area and prefix with static
is called as static Variable
➢Static variables or static Method are Accesed
in Other classes with the help of Class Name.
Static Methods.
Any Method prefix with static is called as
static Methods
Static Initializers
➢It helps to execute the Begenning Instructions
➢It always executes before main Method
➢We are having 2 Types oof static Initializers ie..
o Single line static initializers
o MultiLine static Initializers
Non-static
➢The resource which is not same for all the
Objects should be non- static
Example
Student -100 student
Name<non-static>
Id<Non-static>
Marks<Non-static>
School<static>
Mobile---5
Name<non-static>
Price<Non-static>
Owner=Tabrez <static>
Employee -20 employees
Eid<non-static>
Name<non-static>
Sal<non-static>
Company<static>
➢We are having 3 types of members in non-
static
o Non-static variables
o Non-static Methods
o Non-static Initializers.
▪ Single Line Non-static Initializers
▪ MultiLine Non static Initializers
Non-static Variables
➢Any Global Variable which is Declared or
Declared and Initialized in Global Area is
Called as Non-static Variables
➢We Can’t Access Non-static Resouce with
ClassnName
➢Non-static Method
o Any Method which is Not Prefix with static
is called as Non-static Method
➢Non-static Initializer :
➢They will execute themselves After Object
Creation
➢We are Having 2 Types ie..
o Single Line Non-static Initializer
o MultiLine Non-static Initializer
Memmory
Static Members
ClassBlock
➢Class Block Present in class Static Area
➢Class Block is Created by class Keyword
➢Static members inside the class Block is
Loaded by class Loader
Non-static Members
Object Block
➢Object blocks Present in Heap Area
➢Object Block is created by new Keyword
➢Non-static Members Loaded By Constructor