JAVA PART 2
• Fundamental of oops:
Object: It is a Block of Memory which can have multiple variables
and methods in it.
*) Every Object will have reference (Address).
*) An Object represents real-world entity/object.
*) It can store Heterogeneous type of data.
Generally, object is a physical/virtual entity, anything which
occupies space and memory is considered as object.
Ex: Car, laptop, Tv, etc.,
*) Every object will have its own properties (states) & behaviors
(actions) and unique identification (Address) .
Note: states of an object are stored by using the variables and to
store behaviors we use methods in the program.
Note: Since the states & behaviors belongs to one particular object
They are present inside the object, If I need to store object, we
need one dedicated memory location(Container). But we don’t
have any predefined datatype which helps to create such container
to store the object.
Hence, we can design our own datatype with the help of a
class,which can be used as non-primitive datatype.
Note: every class name will be acting as a name of the class and every
class name can be used as non-primitive data-type.
1
→ class:
It is a logical entity, where class gets life only when the object is
created, class is a blue print using which multiple similar objects can be
created
*) class is a keyword.
*) It is used to design a user defined non-primitive datatype to store
the details of object (states and behaviors of the object).
*) It can also be considered as blueprint of an object.
*) We need class for execution of program.
*) In a class we can create members like methods (to store
behaviors of an object) as well as variable (to store the states of
object).
*) Every Class Name can be a Non-primitive datatype.
*) The java language also comes up with lot of built-in classes
i.e., String, System etc.
Ex: class Employee{
// design variables to store states.
// design methods to store behaviors.
}
Note:
*) The non-primitive datatype will have only default values i.e.., null.
*) They will not be having any default size.
*) Since the name of the class itself can be used as a non-primitive
datatype.
*) we can have any members of non-primitive datatype in java.
2
Members of Class: Anything which is declared within class block is
known as members of the class.
Ex: Methods, Variables, Initializers, Constructors etc.
→ The members of the class cab be classified into 2 Types.
1.) Static Members of class.
2.) Non-Static Members of class.
1.) Static Members of class:
Any members of the class which is prefixed with static modifier is
known as static member of class.
→In java we have Following static members.
i) Static Method
ii) Static Variable
iii) Static Initializers.
I ) Static Methods: A method which is prefixed with static modifier is
known as static methods.
*) The static members will have the memory allocated inside class
static area.
*) Since the static members get the memory inside class static area,
they also known as class members.
*) A block which belongs to a static member is known as Static
Context.
*) For executing a Static Context, we will have a frame getting created
inside Stack Area, which is known as Static Frame.
3
*) Every static frame will have a built-in reference pointing to its
class-static area.
*) The static members of the class can be used within the static
context in the following ways.
i.) Directly with the help of members name or member signature.
ii.) with the help of class using Access operator(.).
Access Operator (.):
*) It is a Binary Operator.
*) It is used to access a member present in another class.
→How to use a static-members of one class inside static context of
another class?
We can use static members inside another class with the help of
Class Name along with access operator.
Note:
i) Initial Class: The class which is given as input to java command is
known as initial class.
ii) Dependent Class: A Class used inside another class is known as
dependent class.
ii) Static Variable: A Global Variable which is prefixed with static
modifier / keyword is known as Static Variable.
*) It should be declared inside class block.
Note: A local variable cannot be made static.
4
Points to be Remember:-
1.) A static variable gets its memory allocated inside class static area.
2.) Since all static variables will be present inside class static area,
they are also known as Class Variables.
3.) A static variable will be assigned with default values.
4.) A static variable can be used anywhere within the class also in a
different class.
i) same class: Directly using variable name.
ii) diff class: With the help of Class Name as reference.
5.) A static variable of one class can be accessed inside another class
with the help of Class Name.
6.) In java we can have local variable as well as global variable with
the same name.
7.) In such situations the highest priority will be given the local
variables.
8.) Global variable can be used inside all the members of the class.,
whereas the local variables can be used only inside the block where
it is declared.
5
iii) Static Initializers: Initializers are the members of the class which
are used for initialization purpose.
→In java we have two types of Initializers:
1) Static declare & Initialization statement.
2) Static block.
1.) Static declare & Initialization statement: The static Initializer
gets executed during the loading process of a class.
*) The static initializers gets executed only once for class loading.
Syntax: static datatype identifier= value/exp;
2.) Static block: An Anonymous block which is prefixed with static
modifiers is known as static block.
*) It is also static Initializer.
Syntax:
static
{
// java satements;
}
Note:
The static block gets executed during the loading process of
class (i.e., before the execution of main method).
*) The static block gets executed only once for the loading process of
a class.
6
Characteristics of Static Block:
i) It doesn’t have a name.
ii) It doesn’t have a return type.
iii) It doesn’t have a formal argument.
iv) It cannot accept any inputs from the user.
v) The programmer cannot call it explicitly.
Note: Inside a class we can have more than one static block & they
gets executed in top to bottom order(sequentially).
LOADING PROCESS OF A CLASS
1.) A class static area will be created for given class (initial class).
2.) All the methods ( static as well as non-static ) will be loaded to
method area.
3.) If static methods are present ,its reference will be stored
(Method signature+address) in class static area.
4.) If a static variable is present, it will get its memory allocated
inside class static area with default value.
5.) If static initializers are present, they gets executed
directly(before executing the main() ).
6.) The loading process is said to be complete.
7
2.) Non-Static Members of class:
Any member declared inside class block without prefixing static
modifier is known as Non-static member of a class.
→In java we have following Non-static members
i) Non-static Variable.
ii) Non-static Method.
iii) Non-static Initializers.
iv) Non-static Constructor.
Note:
*) The non-static members gets their memory allocated inside
object.
*) If we need to access any non-static members…. The first thing we
must do is create an object.
→What is Object?
*) An object is a block of memory created during in runtime inside
heap area.
*) The object also known as Instance Of A Class.
*) The process of creating an object is known as INSTANCIATION.
Syntax to create an object:
new constructor call of the class;
Syntax for Constructor call of the class:
ClassName();
8
Therefore the actual syntax of object creation is :
new ClassName();
➔new:
*) new is a keyword.
*) It is a unary operator.
*) It is used to create an object along with constructor of the class.
*) new will create a block of memory inside heap area and it will
return the address of it after the loading process is completed.
*) The type of address generated for the object will always be
specific to its class. The address of object generated will always be in
the below format:
classname@hexadecimal value
→Non-Primitive Datatypes:
*) They are user defined datatypes which are used to store the
following things:
i) The default values of non-primitive datatypes i.e null.
ii) Address of an object.
*) The variables which are created using non-primitive datatypes is
known as Non-primitive variables / Reference Variables.
9
→Non-Primitive Datatypes:
*) They are user defined datatypes which are used to store the
following things:
iii) The default values of non-primitive datatypes i.e null.
iv)Address of an object.
*) The variables which are created using non-primitive datatypes is
known as Non-primitive variables / Reference Variables.
→ Reference Variable: A variable which is created for the non-
primitive datatypes is known as Reference variables.
*) The Reference Variables are used to store address of object.
Syntax to create reference variable: ClassName identifier;
Note:
→Inside static context we can access static members in 3 ways:
i) Directly with the help of member name.
ii) With the help of ClassName & access Operator.
iii) With the help of address of the object.
→Inside a static context we can access non-static members in
following ways:
i) Only with the help of Address of the object.
10
i) Non-static Variable: A variable which is not prefixed with static
modifier is known as non-static variable.
*) They get their memory allocated inside object created in heap
area.
*) Non-static variable can be accessed with the help of object
reference or address.
*) Non-static variables will be allocated in every object of the class
created.
*) Non-static variables initialized with the default values.
ii) Non-static Method: A method which is not prefixed with static
modifier is known as non-static method.
*) Non-static method gets their memory allocated inside object
created in heap area.
*) A block which belongs to non-static method is called as non-static
context.
*) Non-static methods can be accessed inside static context of same
class using Object address/ reference.
*) Non-static Methods will be allocated in every object of the class
created.
*) Inside Non-static context, we can use both static & Non-static
members of the same class directly without any reference.
11
Non-Static Context:
Any block or Any instruction which belongs to Non-static member of
a class is known as Non-static Context.
Note:
1.) We can use the Non-static members of the class inside Non-static
Context directly.
2.) We can use static members of the class inside non-static context
directly.
i) arguments
ii) Constructor.
Note: Whenever we create an object using No-Argument
Constructor, we need to initialize the states using extra-instructions
which is the main disadvantage of No-argument Constructor, i.e.,
we will not be able to initialize the states of the object during object
creation itself.
12
Non-Static –Initializer
→ In java we have following non-static-initializers.
i.) Non-static declare & initialization statement.
ii.) Non-static block.
i.) Non-static declare & initialization statement:
Syntax: Datatype identifier=value/Expression;
ii) Non-static block: An Anonymous block which is not prefixed with
static modifier is known as Non-static block.
*) The non-static blocks gets executed during loading process of
object i.e., it gets executed for each & every object created.
*) If we have more than one non-static block inside the class, they
gets executed in top to bottom order.
Purpose of Non-static block:
1.) The Non-static block is use to count the number of objects
created.
2.) Usage of non-static block in the Realtime, The Non-static blocks
are used to generate the Id’s automatically to the object
13
14