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

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 Thread creation

What is a thread and how to create a thread in java ?

Thread :
A Thread is a part of program that is a program can contain more than one thread which have separate path of
execution in memory and can work more than one task simultaneously.
A thread can create two way in java :

  • By extending Thread class
  • By implementing Runnable interface

By extending Thread class :
In this way you will simply extends Thread class and override its run() method after that just create object of main class
in main() method and call start() method. You can better understand with below example.


package com.pkjavacode.com;

public class ThreadExample extends Thread {
public void run() {
System.out.println("Run method executing...");
}

public static void main(String arg[]) {
ThreadExample t = new ThreadExample();
t.start();

}
}

 

Output :
Run method executing…

 

By implementing Runnable interface :

In this way you will simply implements Runnable interface and override its run() method after that just create object of
main class in main() method and create Thread class object and pass main class object into Thread constructor and call
start() method. You can better understand with below example.


package com.pkjavacode.com;

public class ThreadExample implements Runnable {
public void run() {
System.out.println("Run method executing...");
}

public static void main(String arg[]) {
ThreadExample te = new ThreadExample();
Thread t=new Thread(te);
t.start();

}
}

 

Output :

Run method executing…

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.

Unicode System in java

Unicode System :

Unicode is a universal international standard character encoding which is capable of representing all the world’s written
languages.
Why java uses Unicode System?
Prior Unicode system, there were many language standards:

  • ASCII : It stand for American Standard Code for Information Interchange used for the United States.
  • ISO 8859-1 : It was used for Western European Language.
  • KOI-8 : Used for Russian.
  • GB18030 and BIG-5 : Used for chinese, and so on.

With these languages standard arise two problems:

  • A particular code value corresponds to different letters in the various language standards.
  • The encodings for languages with large character sets have variable length. Some common characters are encoded as single bytes, other require two or more byte.

To solve these problems, a new language standard was developed i.e. Unicode System.

In unicode, character holds 2 byte, so java uses 2 byte for characters.

  • lowest value are: \u0000 and
  • highest value are: \uFFFF

What is IS-A and HAS-A relationship in java

IS-A and HAS-A relationship in java :

IS-A relationship :

IS-A relationship is based on class inheritance or interface implementation. You can say that “this thing is a type of that
thing”.
Example :

Suppose you have a class Vehicle and another class Car extending Vehicle class. So you can say that a Car is a
Vehicle i.e. Car IS-A Vehicle.

Syntax :


public class Vehicle{
//some code here!
}

public class Car extends Vehicle{
//some code here!
}

 

HAS-A relationship :

It is based on usage rather than inheritance. Suppose a class Vehicle and other class Car which extending Vehicle class and
in class Car contain a reference variable of class Vehicle like below..


public class Vehicle{
//some code here!
}

public class Car{
private Break mybreak;
//some logic here!
}

In above code class Car has an instance variable type of Break. So you can say ..
Car HAS-A Break.

Note : HAS-A relationship allows us to design classes that follow good OO practices which help reduce bugs.