[go: up one dir, main page]

0% found this document useful (0 votes)
33 views13 pages

Static Variable, Static Method Recursion

The document explains static variables and methods in Java, highlighting their characteristics, advantages, and applications, such as memory efficiency and global access. It also covers recursion, defining it as a process where a function calls itself, and discusses its properties, advantages, and example code for calculating factorials. Additionally, it includes practice exercises for implementing recursion in Java.

Uploaded by

KILLER GAMING
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views13 pages

Static Variable, Static Method Recursion

The document explains static variables and methods in Java, highlighting their characteristics, advantages, and applications, such as memory efficiency and global access. It also covers recursion, defining it as a process where a function calls itself, and discusses its properties, advantages, and example code for calculating factorials. Additionally, it includes practice exercises for implementing recursion in Java.

Uploaded by

KILLER GAMING
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

STATIC VARIABLE,

STATIC METHOD &


RECURSION
Dr. Karthigai Selvi
WHAT IS STATIC VARIBLE
it means that the variable belongs to the
class itself rather than to any specific
instance of the class.
This means that there is only one copy of
the variable in memory, regardless of
how many instances of the class are
created.
SAMPLE PROGRAM
public class staticvar
{
int a=10;
static int b=10;
void count()
{
System.out.println("Instance Variable a: "+(a++));
System.out.println("Static Variable b: "+(b++));
}
public static void main(String[] s)
{
staticvar c1=new staticvar();
c1.count();
c1.count();
staticvar c2=new staticvar();
c2.count();
c2.count();
}
}
ADVANTAGES OF USING
STATIC VARIBALES Class: static var
static int b=10
count()

Obj: c1 Obj: c2
int a=10 int a=10
count() count()

 They save memory by being shared among all instances.


 They can be accessed directly without object instantiation.
 They maintain values across instances, ensuring data
sharing and preservation.
APPLICATION AREAS OF
USING STATIC VARIABLE
 when you need to share same variable for
every instance of class then you should use
static variable.
 Ticket booking – available sheet notification in
bus, train or air ticket booking
 Memory management – Return available
memory area
STATIC METHOD
A static method in Java is a method that is part of a class
rather than an instance of that class.
 Every instance of a class has access to the method. Static
methods have access to class variables (static variables)
without using the class's object (instance).
 Only static data may be accessed by a static method.
 Syntax

static int methodname()


{
---
}
--
Classname.methodname(5);
EXAMPLE CODE
public class staticmethod
{
int a=10;
static int b=10;
static void multiple()
{
// System.out.println("Instance Variable a: "+(a*=a));
System.out.println("Static Variable b: "+(b*=b));
}
void count()
{
System.out.println("Instance Variable a: "+(a++));
System.out.println("Static Variable b: "+(b++));
}
public static void main(String[] s)
{
staticmethod c1=new staticmethod();
c1.count();
staticmethod.multiple();
}
}
ADVANTAGES OF USING
STATIC METHOD
 Static variables and methods in Java provide
several advantages,
 including memory efficiency, global access,
object independence, performance, and code
organization.
WHAT IS JAVA RECURSION
 Recursion is defined as a process which calls itself
directly or indirectly and the corresponding function is
called a recursive function.
PROPERTIES OF
RECURSION:
 The primary property of recursion is the ability to solve
a problem by breaking it down into smaller sub-
problems, each of which can be solved in the same way.
 A recursive function must have a base case or stopping
criteria to avoid infinite recursion.
 Recursion involves calling the same function within
itself, which leads to a call stack.
 Recursive functions may be less efficient than iterative
solutions in terms of memory and performance.
EXAMPLE CODE
public class Recursion1 {
static int factorial(int n){
if (n == 1)
return 1;
else
return(n * factorial(n-1));
}

public static void main(String[] args) {


System.out.println("Factorial of 5 is: "+factorial(5));
}
}
ADVANTAGES OF
RECURSION:
 Recursion can simplify complex problems by breaking
them down into smaller, more manageable pieces.
 Recursive code can be more readable and easier to
understand than iterative code.
 Recursion is essential for some algorithms and data
structures.
 Also with recursion, we can reduce the length of code
and become more readable and understandable to
the user/ programmer.
PRACTICE
 Write Java program to find sum of a series 1+2+3+….
+500 using recursive function
 Print first 100 numbers from fibonacci series using
recursive function 1,1,2,3,5,8,….

You might also like