UNIT- I
CHAPTER IN BOOK- 7
PART- II
-K. Indhu
SYLLABUS COVERED HERE
Methods:- Argument Passing into
Methods
Methods Returning Objects
Introducing Access Specifier-public,
private
Understanding static
Understanding final
K. INDHU
GOALS
1.
2.
3.
4.
Methods:- Argument Passing into Methods
Methods Returning Objects
Introducing Access Specifier- (i) public, (ii) private
Understanding1. Static Variable,
2. Static Method,
3. Static Block
5. Example Programs for Static Members
6. Understanding1. Final Class,
2. Final Variable,
3. Final Method.
K. INDHU
ARGUMENT PASSING- CALL BY
VALUE
K. INDHU
ARGUMENT PASSING BY
REFERENCE
K. INDHU
METHOD RETURNING OBJEC
K. INDHU
RECURSION POSSIBLE IN JAVA
K. INDHU
INTRODUCING ACCESS
SPECIFIERS
K. INDHU
INTRODUCING ACCESS
SPECIFIERS
K. INDHU
UNDERSTANDING STATIC
At time we will want to define a class member that will be
used independently of any object of that class.
However, it is possible to create a member that can be
accessed thru class without thru object.
To create such a member, precede its declaration with the
keyword static.
We can declare both methods and variables to be static.
The most common example of a static member is main( ).
main( ) is declared as static because it must be called
before any objects exist.
K. INDHU
JAVA STATIC MEMBER
DEFINITION-> If you declare any variable/method with static modifier, it
is known static variable/method.
EXAMPLE->
MEANING->
(1) The static variable can be used to refer the common
property of all objects (that is not unique for each object).
(2) Shared Single Memory is allocated to static variable /
method, irrespective of how many objects are created.
(3) Static Variables/Methods can be accessed using Classname
as<class-name>.<static-variable-name>
<class-name>.<static-method-name>
static String companyname;
public void static main(String args[]){ }
ADVANTAGE-> STATIC VARIABLE SAVES MEMORY.
K. INDHU
REGARDING JAVA STATIC
Instance variablesMEMBER
declared as static are,
essentially, global variables.
Instead, all instances of the class share the
same static variable.
Methods declared as static have several
restrictions: They can only call other static methods.
They must only access static data.
They cannot refer to this or super in any
way.
K. INDHU
STATIC MEMBER EXAMPLE
K. INDHU
STATIC MEMBER
ANOTHEREXAMPLE
K. INDHU
REGARDING JAVA STATIC
WHY JAVA MAINMEMBER
METHOD IS STATIC?
=> (1) Because object is not required to
call static method.
=> (2) If it were non-static method, jvm
create object first then call main()
method.
=> (3) Creating object for main() will lead
the problem of extra memory allocation.
K. INDHU
STATIC BLOCK
K. INDHU
UNDERSTANDING FINAL
Declaring a variable as "final" prevents its
contents from being modified hence
forth.
We must initialize a final variable when it
is declared, itself.
Thus->
If you make any variable as final,
you cannot change the value of final
variable(It will be constant).
K. INDHU
FINAL VARIABLE EXAMPLE
K. INDHU
UNDERSTANDING FINAL
METHOD
K. INDHU
UNDERSTANDING FINAL CLASS
K. INDHU
UNDERSTANDING FINAL
Blank Final Variable is a final variable that is not initialized at
the time of declaration.
A blank final variable can be initialized only in constructor.
Once the blank final variable is initialized in constructor, it
can not be changed hence forth.
K. INDHU
SO FAR WE STUDIED
Methods:- Argument Passing into
Methods
Methods Returning Objects
Introducing Access Specifier-public,
private
Understanding static
Understanding final
K. INDHU
HAPPY
LEARNING!!!