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

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.

Enumeration or enums in java

Enumeration or enums :

Enumeration is just like a list which contains only constant type value. The items in the enumerated list are called enums.
Using enums, can help to reduce the bugs in from your code. The basic components of an enum are its constants.
Enums can be declared as their own class or as a class member while we can’t declare within any methods.

Example :
Here you will see enum declaration outside of a class.


package com.pkjavacode.com;

enum Fruit {
APPLE, MANGO, BANANA, ORANGE
}

public class EnumExample {

private Fruit name;

/**
* @auther pradeep
*/
public static void main(String[] args) {
// fruit name;
EnumExample fn = new EnumExample();
fn.name = Fruit.APPLE;
System.out.println(fn.name);
}
}

Output :
APPLE

Note : in above example enum is declared outside of a class so you can not declare with private or protected modifier
so it is must be public or default.

You can also see an enum declaration within a class.

Example :


package com.pkjavacode.com;

public class EnumExample {
enum Fruit {
APPLE, MANGO, BANANA, ORANGE
}

private Fruit name;

/**
* @auther pradeep
*/
public static void main(String[] args) {
// fruit name;
EnumExample fn = new EnumExample();
fn.name = Fruit.APPLE;
System.out.println(fn.name);
}
}

Output :
APPLE

Note : enums value can’t accept a String or int type.
Example :


enum Fruit {"apple", "mango", "banana"}

The above declaration of enum is not legal and if you will try to do that you will get syntax error by the compiler.

Declaring Constructors, Methods and Variables in an enum :

An enum is a kind of class so you can add constructor, method and variable in an enum just like you add these other
classes in java.

Example :


package com.pkjavacode.com;

enum Fruit {
APPLE(3), BANANA(12), MANGO(2);
// declare a constructor
Fruit(int number) {
this.number = number;
}

// declare an instance variable
private int number;

// declare a method
public int getNumber() {
return number;
}
}

public class EnumExample {

Fruit name;

/**
* @author pradeep
*/
public static void main(String[] args) {
// fruit name;
EnumExample fn = new EnumExample();
fn.name = Fruit.APPLE;
System.out.println("Number of apple are :" + fn.name.getNumber());
}
}

Output :
Number of apple are : 3

 

Advantages of Hibernate Framework

Advantages of  Hibernate Framework :

  • Hibernate has capability to generate primary keys automatically while storing the records into database.
  • Hibernate provide automatic table creation facility.
  • Hibernate framework supports Inheritance, Associations, Collections.
  • Hibernate has annotations facilities.
  • In hibernate if you save the subclass class object, then its super class object will also be stored into the database, that is hibernate supporting inheritance.
  • Hibernate provide caching technique through this, an application can increase its performance automatically.
  • Hibernate supports relationships such as One-To-Many,One-To-One, Many-To-Many-to-Many, Many-To-One
    it is also supports collections like list, set, map, bag.
  • In hibernate you only have Un-checked(Runtime) exceptions, so don’t need to write try-catch block, or throws. Because in hibernate has translator which converts checked to Un-checked.
  • Hibernate has its own query language, that is hibernate query language(HQL) which is database independent.┬áSo that if you will change the database, then your application will works properly due to HQL is database independent.
  • Pagination can use in hibernate is very simple.
  • Hibernate provides Dialect classes, so you don’t need to write sql specific queries in hibernate, instead use the methods provided by API.