Tuesday, June 28, 2016

Inner Class and static class in java

Can a class be static in java?
The answer is YES, we can have static class in java. In java, we have static instance variables as well as static methods and also static block. Classes can also be made static in Java.
Java allows us to define a class within another class. Such a class is called a nested class. The class which enclosed nested class is known as Outer class. In java, we can’t make Top level class static. Only nested classes can be static.

What are the differences between static and non-static nested classes? 
Following are major differences between static nested class and non-static nested class. Non-static nested class is also called Inner Class.
1) Nested static class doesn’t need reference of Outer class, but Non-static nested class or Inner class requires Outer class reference.
2) Inner class(or non-static nested class) can access both static and non-static members of Outer class. A static class cannot access non-static members of the Outer class. It can access only static members of Outer class.
3) An instance of Inner class cannot be created without an instance of outer class and an Inner class can reference data and methods defined in Outer class in which it nests, so we don’t need to pass reference of an object to the constructor of the Inner class. For this reason Inner classes can make program simple and concise.

package com.javadsalgo.innerclass;

/* Java program to demonstrate how to implement static and non-static
 classes in a java program. */
class OuterClass {
      private static String msg = "JavaDsAlgo";

      public static class NestedStaticClassWithStaticMethod {

// Only static members of Outer class is directly accessible in nested
            // static class
            public  void printMessage() {

// Try making 'message' a non-static variable, there will be
                  // compiler error
                  System.out.println("Message from nested static class with static method: " + msg);
            }
      }
     
      // Static nested class
      public static class NestedStaticClass {

// Only static members of Outer class is directly accessible in nested
            // static class
            public void printMessage() {

// Try making 'message' a non-static variable, there will be
                  // compiler error
                  System.out.println("Message from nested static class: " + msg);
            }
      }

      // non-static nested class - also called Inner class
      public class InnerClass {

// Both static and non-static members of Outer class are accessible in
            // this Inner class
            public void display() {
                  System.out.println("Message from non-static nested class: " + msg);
            }
      }
}


public class Main {
      // How to create instance of static and non static nested class?
    public static void main(String args[]){
       
       // create instance of nested Static class
       OuterClass.NestedStaticClass printer = new OuterClass.NestedStaticClass();
        
       // call non static method of nested static class
       printer.printMessage();  

       // In order to create instance of Inner class we need an Outer class
       // instance. Let us create Outer class instance for creating
       // non-static nested class
       OuterClass outer = new OuterClass();       
       OuterClass.InnerClass inner  = outer.new InnerClass();
       
       // calling non-static method of Inner class
       inner.display();
       
       // we can also combine above steps in one step to create instance of
       // Inner class
       OuterClass.InnerClass innerObject = new OuterClass().new InnerClass();
       
       // similarly we can now call Inner class method
       innerObject.display();
    }
}

http://www.geeksforgeeks.org/static-class-in-java/


Tuesday, June 21, 2016

Differnce between Web application & Web Service

Web Application
Web Service
Web application is for Users.
Web services are for machine/program to machine/program communication. Web services are intended for other                   applications
Web applications usually present data in HTML which looks nice to the user
Web services usually present data in        XML which easy to parse by other applications.
Eg. Calculator is an web application that uses add,subtract and many other webservices in combine.
Eg. To add 2 no.s we write a web service, to subtract we write a diffrent web service

                                   
Lets take an example of Google search. We can use Google search in two ways. First, we can visithttp://www.google.com and put out query for search. Google the returns the result. Second, we can integrate Google Search in our websites with custom search API.
In first case Google Search is acting as web application while in second example it is acting as web service.




Tuesday, June 14, 2016

Embeddable property in Hibernate

package com.javadsalgo.model;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity(name="USERDETAILS")
public class UserDetails {

      @Id
      private int id;
      private String name;
      private Address address;

      public Address getAddress() {
            return address;
      }

      public void setAddress(Address address) {
            this.address = address;
      }

      public int getId() {
            return id;
      }

      public void setId(int id) {
            this.id = id;
      }

      public String getName() {
            return name;
      }

      public void setName(String name) {
            this.name = name;
      }

}






package com.javadsalgo.model;

import javax.persistence.Embeddable;
import javax.persistence.Entity;

@Embeddable
public class Address {
     
      private String streetName;
      private String building;

      public String getStreetName() {
            return streetName;
      }

      public void setStreetName(String streetName) {
            this.streetName = streetName;
      }

      public String getBuilding() {
            return building;
      }

      public void setBuilding(String building) {
            this.building = building;
      }

}

package com.javadsalgo.Client;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.javadsalgo.model.Address;
import com.javadsalgo.model.UserDetails;

public class HibernateTestEmbeddable {

      public static void main(String[] args) {
            UserDetails user = new UserDetails();
            user.setName("Paras");

            Address add = new Address();
            add.setBuilding("Twin Tower");
            add.setStreetName("Strret no 5");
            user.setAddress(add);

            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
            Session session = sessionFactory.openSession();
            session.beginTransaction();
            session.save(user);
            session.getTransaction().commit();
            session.close();

      }
}



package com.javadsalgo.model;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity(name = "USERDETAILS1")
public class UserDetails1 {

      @Id
      private int id;
      private String name;

      @Embedded
      @AttributeOverrides({
                  @AttributeOverride(name = "streetName", column = @Column(name = "HOME_STREET_NAME")),
                  @AttributeOverride(name = "building", column = @Column(name = "HOME_BUILIDING")) })
      private Address homeAddress;

      @Embedded
      private Address officeAddress;

      public Address getHomeAddress() {
            return homeAddress;
      }

      public void setHomeAddress(Address homeAddress) {
            this.homeAddress = homeAddress;
      }

      public Address getOfficeAddress() {
            return officeAddress;
      }

      public void setOfficeAddress(Address officeAddress) {
            this.officeAddress = officeAddress;
      }

      public int getId() {
            return id;
      }

      public void setId(int id) {
            this.id = id;
      }

      public String getName() {
            return name;
      }

      public void setName(String name) {
            this.name = name;
      }

}

package com.javadsalgo.Client;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.javadsalgo.model.Address;
import com.javadsalgo.model.UserDetails;
import com.javadsalgo.model.UserDetails1;

public class HibernateTestEmbeddable2 {

      public static void main(String[] args) {
            UserDetails1 user = new UserDetails1();
            user.setName("Paras");

            Address addr = new Address();
            addr.setBuilding("Twin Tower");
            addr.setStreetName("Strret no 5");

            Address addr2 = new Address();
            addr2.setBuilding("New Twin Tower");
            addr2.setStreetName("New Strret no 5");

            user.setHomeAddress(addr);
            user.setOfficeAddress(addr2);
            SessionFactory sessionFactory = new   Configuration().configure().buildSessionFactory();
            Session session = sessionFactory.openSession();
            session.beginTransaction();
            session.save(user);
            session.getTransaction().commit();
            session.close();

      }
}

package com.javadsalgo.model;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class Address {
     
      @Column(name="STREET_NAME")
      private String streetName;
      @Column(name="STREET_BUILIDING")
      private String building;

      public String getStreetName() {
            return streetName;
      }

      public void setStreetName(String streetName) {
            this.streetName = streetName;
      }

      public String getBuilding() {
            return building;
      }

      public void setBuilding(String building) {
            this.building = building;
      }

}