static keyword in java

In java, there are various uses of static keyword few of them you can see with example.

static variable :

It is used in java for a single copy in class.

Example :


package com.pkjavacode.com;

/**
*
* @author pradeep
*/
public class StaticExample {

static String company = "Infosys Limited";
String name;
String id;
long salary;

public StaticExample(String name, String id, long salary) {
this.name = name;
this.id = id;
this.salary = salary;
}

public static void main(String arg[]) {
StaticExample se = new StaticExample("Pradeep Yadav", "IL981243", 40000);
System.out.println("Employee Name :" + se.name + "\nEmployee Id :" + se.id + "\nEmployee Salary :" + se.salary + "\nCompany Name :" + company);
}
}

Output :

Employee Name :Pradeep Yadav
Employee Id :IL981243
Employee Salary :40000
Company Name :Infosys Limited

—————————————————————–
static block :

When you need to print something before main method called used static block of code.

Example :

package com.pkjavacode.com;

/**
*
* @author pradeep
*/
public class StaticExample {

static String company = "Infosys Limited";
String name;
String id;
long salary;

public StaticExample(String name, String id, long salary) {
this.name = name;
this.id = id;
this.salary = salary;
}

public static void main(String arg[]) {
StaticExample se = new StaticExample("Pradeep Yadav", "IL981243", 40000);
System.out.println("Employee Name :" + se.name + "\nEmployee Id :" + se.id + "\nEmployee Salary :" + se.salary + "\nCompany Name :" + company);
}

static {
System.out.println("Excellent work by this employee");
//You can also add some logic here!!
}
}

Output :

Excellent work by this employee
Employee Name :Pradeep Yadav
Employee Id :IL981243
Employee Salary :40000
Company Name :Infosys Limited

——————————————————————

Nested static class :

Through nested static class you can define a class within a class and can access their member.

Example :


package com.pkjavacode.com;

/**
*
* @author pradeep
*/
public class StaticExample {

public static void main(String arg[]) {
StaticDemo sd = new StaticDemo();
sd.display();

}

static class StaticDemo {
//Can add some method and variable here!!

void display() {
System.out.println("Nested static class method");
}
}
}

Output :

Nested static class method

—————————————————————————————————-

static method :

When you need to access a method without creating class object used static keyword in method signature.

Example :


package com.pkjavacode.com;

/**
*
* @author pradeep
*/
public class StaticExample {

public static void show() {
System.out.println("static method print");
}

public static void main(String arg[]) {
StaticExample.show();
}
}

Output :

static method print

Find fibonacci series in java

Fibonacci Series :


package com.pkjavacode.com;

/**
*
* @author pradeep
*/
public class FibonacciSeriesExample {

static int fib(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return (fib(n - 1) + fib(n - 2));
}
}

public static void main(String[] args) {
int n = 5;
System.out.println("Fibonacci Series are :");
for (int i = 0; i <= n; i++) {
System.out.print("\t" + FibonacciSeriesExample.fib(i));
}
}
}

Output :

Fibonacci Series are :
1 1 2 3 5 8

Find factorial with recursion

Find factorial with recursion method :

recursion is a process where a method called within itself.


package com.pkjavacode.com;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
*
* @author pradeep
*/
public class FactorialExample {

int fact(int n) {
if (n == 0) {
return 1;
} else {
return n * fact(n - 1);
}
}

public static void main(String[] args) {
FactorialExample f = new FactorialExample();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("Enter a number :");
int b = Integer.parseInt(br.readLine());
int a = f.fact(b);
System.out.println("Factorial are :" + a);
} catch (IOException e) {
e.printStackTrace();
}
}
}

Output :

Enter a number :
5
Factorial are :120