2. static vs instance
• Static – Assign memory once in life.
• Static variable or static method can call inside static or
non-static area.
• Use static keyword to declare static, method or variable
• Using static, can call directly with class name
ex. ClassName.StaticMethodOrVariable;
• Non- static (Instance) – non-static variable or method are
called instance variable or instance method.
• No one keyword, for declare instance.
• Instance is accessed only, non-static area.
Class
Instance
static
Attributes
Instance
static
Methods
2
www.infoviaan.com
3. static with variable
public class StaticVariable{
static int x = 789; //static variable
String name = “Laddu”; //non-static (instance) variable
public static void main(String args[]) {
StaticVariable sv = new StaticVariable();
System.out.println(sv.x);
System.out.println(StaticVariable.x);
System.out.println(sv.name);
System.out.println(StaticVariable.name);
//Compile Time Error
}
}
3
www.infoviaan.com
4. static block
• Static block is used for initializing the static variables.
• This block gets executed when the class is loaded in the
memory.
• A class can have multiple Static blocks, which will
execute in the same sequence in which they have been
written into the program.
4
www.infoviaan.com
5. Program - static block
public class TestStaticBlock {
static int x = 10;
public static void main(String[] args) {
System.out.println(x+10 +" From Main Method");
}
static {
System.out.print(x + " From Static block n");
}
}
10 From Static block
20 From Main Method
// Executes before main method
// Executes After static block
5
www.infoviaan.com
6. static class
• Static classes are sealed and therefore cannot be
inherited.
• They cannot inherit from any class except Object.
• Static classes cannot contain an instance constructor.
however, they can contain a static constructor.
• It is Nested class
6
www.infoviaan.com
7. Use of static
static
class
method
variable Only one time memory allocation
Can be call with Object,
Without Object, and with class name also
Nested class
7
www.infoviaan.com
8. Function in OOP - Method
• Method – Perform specific operation.
• Categorised into 4 types
1.No Return value and No Arguments
2.No Return value with Arguments
3.Return Value but No Arguments
4.Return Value with Arguments
• Object - Memory instantiation.
8
www.infoviaan.com
9. No Return value and No Arguments
void display( )
{
System.out.println(“Hello! This is Display method”);
}
static void sum()
{
int a,b,c; // Local variable
a = 45;
b = 35;
c = a + b;
System.out.println(“Sum Result is : ”+ c);
}
public static void main(String args[]){
sum(); //No CE, static method
display(); //Compilation error, method is not static
} 9
www.infoviaan.com
10. No Return value with Arguments
public class MethodArgument{
static void info(int age, String name ) {
System.out.println(“Hello! My name is: ”+ name + “n My
age is: ”+ age);
}
void div(double x, int y){
double z = x/y;
System.out.println(“Division Result is: ”+ z);
}
public static void main(String args[]) {
info(25, “Ramdev”);
MethodArgument obj; //object declaration
obj = new MethodArgument(); //object instantiation
obj.div(1123.50, 50); //method calling, used with
object
}
} 10
www.infoviaan.com
11. Return Value but No Arguments
public class MethodReturn{
String info() {
String fullInfo = “Hello! My name is: Ramdev n My age
is: 60 ”
return fullInfo;
}
int multilply(){
int x=78, y=10;
return x*y;
}
public static void main(String args[]) {
MethodReturn m = new MethodArgument();
String s = m.info();
System.out.println(s);
System.out.println(“Result of Multiplication: ” +m.multiply());
}
} 11
www.infoviaan.com
12. Return Value with Arguments
public class A {
double area(double radius){
return 3.1415*radius*radius;
}
String details(String name, String address, int age, double salary){
String complete = “Name is : ”+name + “n Age is :”+age + “n
Salary is : ”+ salary+ “n Address is: ”+address;
return complete;
}
public static void main(String args[]) {
A a = new A();
A ob = new A();
System.out.println (“Area of Circle is : ” +a.area(2.5));
System.out.println (“Area of Circle is : ” +b.area(3.2));
System.out.println(a.details(“Mark jukerburg”, “USA”,
32, 36872782.8907));
}
} 12
www.infoviaan.com
13. Calculator
public class Calculator {
int a, b, c; //instance/class variable
public void sum(){
a = 89; b = 78;
c = a + b ;
System.out.println(“Sum Result is: ”+ c);
}
public void subtract(int x, int y){
a= x-y;
System.out.println(“Subtraction Result is: ”+ a);
}
public double division(){
double a,b;
a = 123.678;
b = 5.8;
return a/b;
} 13
www.infoviaan.com
14. Calculator cont.
public double multiply(double d1, double d2){
return d1*d2;
}
public static void main(String args[]) throws IOException {
Calculator obj = new Calculator();
int choice;
do{
System.out.println(“Enter a for Additionn Enter s for
Subtraction n Enter m for Multiplication n Enter d
for Division n Enter e for Exit ”);
choice = System.in.read();
switch(choice){
case ‘a’ : obj.sum(); break;
case ‘s’ : obj.subtract(356, 89 ); break;
case ‘d’ : System.out.println(“Division Result: ”+obj.div(); break;
case ‘m’: System.out.println(“Multiplication Result:
” + obj.multiply(123.34, 78.90); break;
case ‘e’: break; }
}while(choice!=‘e’);
}
} 14
www.infoviaan.com
15. QA
1. Difference between static & instance?
2.How to execute statement before main
method?
3.Why function are called method in OOP?
4.What is static block & class explain?
5.What is object declaration & instantiation?
15
www.infoviaan.com