SlideShare a Scribd company logo
3
Most read
4
Most read
5
Most read
Method, Static
1
www.infoviaan.com
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Get in Touch
Thank You
www.infoviaan.com
16
www.infoviaan.com

More Related Content

PPTX
I/O Streams
PPT
PPTX
INHERITANCE IN JAVA.pptx
PPTX
Type casting in java
PPTX
Super Keyword in Java.pptx
PDF
Generics
PPTX
Access modifier and inheritance
PPTX
Polymorphism presentation in java
I/O Streams
INHERITANCE IN JAVA.pptx
Type casting in java
Super Keyword in Java.pptx
Generics
Access modifier and inheritance
Polymorphism presentation in java

What's hot (20)

PPTX
Inter Thread Communicationn.pptx
PPTX
Java class,object,method introduction
PPTX
Control statements in java
PPTX
Static Members-Java.pptx
PPTX
Dynamic method dispatch
PPTX
Inner classes in java
PPT
Final keyword in java
PPT
Java static keyword
PPTX
Java Data Types
PPT
Abstract class in java
PDF
Java variable types
PPT
Generics in java
PDF
Polymorphism In Java
PPTX
Constructor in java
PDF
Java I/o streams
PPTX
Static Data Members and Member Functions
PPTX
Packages in java
PPTX
Introduction to java
PPTX
Abstract Class & Abstract Method in Core Java
PPTX
java interface and packages
Inter Thread Communicationn.pptx
Java class,object,method introduction
Control statements in java
Static Members-Java.pptx
Dynamic method dispatch
Inner classes in java
Final keyword in java
Java static keyword
Java Data Types
Abstract class in java
Java variable types
Generics in java
Polymorphism In Java
Constructor in java
Java I/o streams
Static Data Members and Member Functions
Packages in java
Introduction to java
Abstract Class & Abstract Method in Core Java
java interface and packages
Ad

Similar to Java Method, Static Block (20)

PDF
OOPs & Inheritance Notes
PPTX
Lecture_4_Static_variables_and_Methods.pptx
PPTX
Static keyword ppt
PPTX
6. static keyword
PPT
Explain Classes and methods in java (ch04).ppt
PPT
Ch. 6 -Static Class Members.ppt programming
PPTX
Simple class and object examples in java
PPTX
Lecture 6.pptx
PPTX
PPTX
Class as the Basis of all Computation.pptx
PPT
Core java concepts
PPTX
Object oriented concepts
PPTX
PPTX
Class and Object.pptx from nit patna ece department
PDF
Class and Object JAVA PROGRAMMING LANG .pdf
PDF
Java - Class Structure
PPTX
3.Classes&Objects.pptx
PPT
Java Concepts
PPT
Core Java
PPTX
OOPJ_PPT1, Inheritance, Encapsulation Polymorphism Static Variables.pptx
OOPs & Inheritance Notes
Lecture_4_Static_variables_and_Methods.pptx
Static keyword ppt
6. static keyword
Explain Classes and methods in java (ch04).ppt
Ch. 6 -Static Class Members.ppt programming
Simple class and object examples in java
Lecture 6.pptx
Class as the Basis of all Computation.pptx
Core java concepts
Object oriented concepts
Class and Object.pptx from nit patna ece department
Class and Object JAVA PROGRAMMING LANG .pdf
Java - Class Structure
3.Classes&Objects.pptx
Java Concepts
Core Java
OOPJ_PPT1, Inheritance, Encapsulation Polymorphism Static Variables.pptx
Ad

Recently uploaded (20)

PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Yogi Goddess Pres Conference Studio Updates
PPTX
Lesson notes of climatology university.
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Cell Structure & Organelles in detailed.
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Computing-Curriculum for Schools in Ghana
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Trump Administration's workforce development strategy
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Pharma ospi slides which help in ospi learning
Yogi Goddess Pres Conference Studio Updates
Lesson notes of climatology university.
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Orientation - ARALprogram of Deped to the Parents.pptx
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Cell Structure & Organelles in detailed.
Microbial diseases, their pathogenesis and prophylaxis
GDM (1) (1).pptx small presentation for students
Computing-Curriculum for Schools in Ghana
O5-L3 Freight Transport Ops (International) V1.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Trump Administration's workforce development strategy
202450812 BayCHI UCSC-SV 20250812 v17.pptx
VCE English Exam - Section C Student Revision Booklet
O7-L3 Supply Chain Operations - ICLT Program
Chinmaya Tiranga quiz Grand Finale.pdf
Final Presentation General Medicine 03-08-2024.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...

Java Method, Static Block

  • 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
  • 16. Get in Touch Thank You www.infoviaan.com 16 www.infoviaan.com