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

Advertisements

Java Generic

Java Generic :

A parameterized type that operates by a class or an interface is called generic. It is introduced in java-5. It facilitate to write algo which is independent from any specific data type and type safety.

Through Generic its possible to create a single class or method that automatically works with all data types such as Integer, String Float, Double etc. It provide ability to reuse existing code safely and easily.

Note : Generic works only with Object type such as Integer, Float. It can’t work with primitive type.

Example :

You can better understand Generic through below code :


package com.pkjavacode.com;

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

public static void main(String args[]) {

GenericExample<Integer> gt = new GenericExample(50);
int a = gt.getObject();
System.out.println(a);
GenericExample<String> gt2 = new GenericExample("Pradeep Yadav");
String b = gt2.getObject();
System.out.println(b);
}
}

class GenericExample<T> {

T obj;

public GenericExample(T obj2) {
obj = obj2;
}

public T getObject() {
return obj;
}
}

Output :

50
Pradeep Yadav

vector collection in java


package com.pkjavacode.com;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class VectorExample {
public static void main(String args[]) {
List<String> list = new Vector<String>();
list.add("Pradeep");
list.add("Ajit");
list.add("Vijay");
Iterator<String> itr = list.iterator();
while (itr.hasNext()) {
Object element = (Object) itr.next();
System.out.println("" + element);
}
}
}

Output :
Vector elements are :
Pradeep
Ajit
Vijay

java custom exception

java custom exception :

To create custom exception in java just extends Exception class.

Example :


package com.pkjavacode.com;

class MyException1 extends Exception {
private String message;

public MyException1(String message) {

this.message = message;
}

public String getMessage() {
return message;
}
}

public class MyException {
public static void main(String[] a) {
try {
MyException.myTest(null);
} catch (MyException1 mae) {
System.out.println("Inside catch block: " + mae.getMessage());
}
}

static void myTest(String str) throws MyException1 {
if (str == null) {
throw new MyException1("String val is null");
}
}
}

 

Output :
Inside catch block: String val is null

java Singleton class

Create a Singleton class in java :

In java, when you will create a singleton class, you can’t create more than one instance that is only single instance will be created.

Example :


package com.pkjavacode.com;

public class SingletonDemo {

private static SingletonDemo singleinstance = new SingletonDemo();

private SingletonDemo() {

}

private static SingletonDemo getInstance() {

if (singleinstance == null) {
singleinstance = new SingletonDemo();

System.out.println("AccountCreation Class Object creatred...!!!");
} else {
System.out
.println("AccountCreation Class Object not Creatred just returned
Created one...!!!");
}
return singleinstance;
}

public void create(int no) {
System.out.println("Account Created Successfully, with Number:" + no);
}

public static void main(String[] args) {

SingletonDemo sd = SingletonDemo.getInstance();
SingletonDemo sd2 = SingletonDemo.getInstance();
sd.create(10);
sd2.create(20);
}
}

Output :

AccountCreation Class Object not Creatred just returned Created one…!!!
AccountCreation Class Object not Creatred just returned Created one…!!!
Account Created Successfully, with Number:10
Account Created Successfully, with Number:20

check a number is armstrong or not in java

check a number is armstrong or not  in java :


package com.pkjavacode.com;

public class ArmstrongDemo {

/**
* @auther pradeep
*/
public static void main(String[] args) {

int n = 153, c = 0, a, d;
d = n;
while (n > 0) {
a = n % 10;
n = n / 10;
c = c + (a * a * a);
}
if (d == c)
System.out.println("It is armstrong number");
else
System.out.println("it is not an armstrong number");
}
}

Output :

It is armstrong number

Coupling and Cohesion in java

Coupling :
Coupling is a degree where a class knows about another class. If class A has about class B, is what class B exposed
through its interface, then class A and B are called loosely coupled. And on other hand if class A relies on parts of class B
that are not part of class B’s interface, then the coupling between the class A and B is tighter which is not a good thing.