5 oop
5 oop
OOPL:
A language that supports all the principles of an object oriented programming is
known as an object oriented programming language.
Class:
A class is a collection of variables & methods.
DataType Variable2;
================
ReturnType MethodName1(arg1, arg2, … )
{
==================
}
}
Class will not occupy memory where as file occupies memory.
Object:
Object is an instance of a class.
new:
It is called as dynamic memory allocation operator and it allocates the memory to
instance variables at run time.
Object occupies memory and object reference also occupies memory.
Object contains data whereas an object reference contains hash code.
Anonymous Object:
Syntax: new Constructor();
Example: new Demo();
It is also called as unreferenced object.
Object reference can be created without object also. That object reference
contains null.
null is an object literal.
Variables:
A variable is a container that contains data.
There are 3 types of variables in Java:
1) Instance Variables
2) Class Variables
3) Local Variables
1) Instance Variables:
A variable that is defined as a member of a class is known as an instance variable.
Memory allocated to instance variables whenever an object is created.
Instance variables are stored in heap area.
2) Class Variables:
A variable that is defined as a static member of a class is known as class variable.
Memory allocated to class variables whenever class is loaded.
Class variables are stored in method area.
3) Local Variables:
A variable that is defined inside a method is known as local variable.
Memory allocated to local variables whenever method is called.
Local variables are stored in stack area.
Note1: Local variables cannot be static in Java.
Note2: No global variables in Java (Outside the class).
Execution Priority:
1) Class Variables
2) Static Blocks
3) Main Method
Class can also be called as reference data type & Object reference can also be
called as reference variable.
Primitive type variable contains data whereas reference type variable contains
hash code.
Separate copy of instance variable exists for every object whereas only one copy
of class variable exists for all objects.
Example:
class Demo
{
int x=10;
static int y=20;
public static void main(String args[])
{
int z=30;
Demo d1=new Demo();
Demo d2=new Demo();
}
}
{
System.out.println(x);
System.out.println(Demo.x);
System.out.println(new Demo().x);
Demo d=new Demo();
System.out.println(d.x);
}
}
int z=10;
for(int i=1;i<=10;i++)
{
=============
}
}
}
In the above example x is an instance variable, y is a class variable, z is a local
variable, args is a method parameter & i is a block variable
Instance variables & Class variables scope is based on access modifiers.
Method parameters & local variables scope is limited to method only.
Block variables scope is limited to block only.
Note: Method parameters & block variables are also called as local variables.
Use instance variable if the value is different for objects.
Use class variable if the value is same for all objects.
Use local variable to perform the task.
By