Thursday 30 April 2015

Java Source World: finalize( ) method

finalize( ) method in java | Java Source World

                                                             Java Source World                A Java quick reference blog




 finalize( ) method:



All the objects have finalize() method, this method is inherited from the Object class.

  •  finalize() is used to release the system resources other than memory(such as file handles & network connections.
  • finalize( ) is used just before an object is destroyed and can be called prior to garbage collection.
  • finalize() is called only once for an Object. If any exception is thrown in the finalize() the object is still eligible for garbage collection.
  • finalize() can be called explicitly. And can be overloaded, but only original method will be called by Garbage-collect.
  • finalize( ) may only be invoked once by the Garbage Collector when the Object is unreachable.

 Continue Reading:
   Implementing equals() and hascode() methods in java
   Equals() and Hashcode()
   What will happen if equals and hashcode not overridden
   What are marker interfaces in java
   Serialization in java


                                            


                                                                Java Source World                A Java quick reference blog

Read More »

Wednesday 29 April 2015

Java Source World: What are access specifiers and access modifiers

What are access specifiers and access modifiers | Java Source World


                                                                                       Java Source World                   A Java quick reference blog


 What are access specifiers and access modifiers in java



          Access specifiers                                      Access modifiers

             Public                                                                         Public
             Protected                                                                  Abstract
             Private                                                                       Final
                                                                                                 Static
                                                                                                 Volatile
                                                                                                 Synchronized
                                                                                                 Transient
                                                                                                  Native

  1.  Public : The Variables and methods can be access any where and any package.
  2.  Protected : The Variables and methods can be access same Class, same Package & sub class.
  3.  Private : The variable and methods can be access in same class only.
Same class - Public, Protected, and Private
Same-package & subclass - Public, Protected
Same Package & non-sub classes - Public, Protected
Different package & Sub classes - Public, Protected
Different package & non- sub classes - Public

Identifiers : are the Variables that are declared under particular Data type.
Literals: are the values assigned to the Identifiers.
Static : access modifier. Signature: Variable-Static int b; Method- static void meth(int x)

When a member is declared as Static, it can be accessed before any objects of its class are
created and without reference to any object. Eg : main(),it must call before any object exit.
  1. Static can be applied to Inner classes, Variables and Methods.
  2. Local variables can’t be declared as static.
  3. A static method can access only static Variables. and they can’t refer to this or super in anyway.
  4. Static methods can’t be abstract.
  5. A static method may be called without creating any instance of the class.
Only one instance of static variable will exit any amount of class instances.
Final : access modifier
  1. All the Variables, methods and classes can be declared as Final.
  2. Classes declared as final class can’t be sub classed.
  3. Method ‘s declared as final can’t be over ridden.
  4. If a Variable is declared as final, the value contained in the Variable can’t be changed.
  5. Static final variable must be assigned in to a value in static initialized block.
Transient : access modifier
  1. Transient can be applied only to class level variables.
  2. Local variables can’t be declared as transient.
  3. During serialization, Object’s transient variables are not serialized.
  4. Transient variables may not be final or static. But the complies allows the declaration and no compile time error is generated.

Volatile : access modifier
  1. Volatile applies to only variables.
  2. Volatile can applied to static variables.
  3. Volatile can not be applied to final variables.
  4. Transient and volatile can not come together.
  5. Volatile is used in multi-processor environments.
Native : access modifier
  1. Native applies to only to methods.
  2. Native can be applied to static methods also.
  3. Native methods can not be abstract.
  4. Native methods can throw exceptions.
  5. Native method is like an abstract method. The implementation of the abstract class and native method exist some where else, other than the class in which the method is declared.
Synchronized : access modifier
  1. Synchronized keyword can be applied to methods or parts of the methods only.
  2. Synchronize keyword is used to control the access to critical code in multi-threaded programming.



    Continue Reading: 
    Declaration of access specifier and access modifiers
      Read:  Java OOPs concepts


                                                           Java Source World                   A Java quick reference blog

      Read More »

      Java Source World: Declaration of access specifier and access modifiers

      Declaration of access specifier and access modifiers | Java Source World




                                                                   Java Source World                   A Java quick reference blog


       Declaration of access specifier and access modifiers


      Class -  Public, Abstract, Final


      Inner Class - Public, Protected, Private, Final, Static,

      Anonymous - Public, Protected, Private, Static

      Variable - Public, Protected, Private, Final, Static, Transient, Volatile, Native

      Method - Public, Protected, Private, Final, Abstract, Static, Native, Synchronized

      Constructor - Public, Protected, Private

      Free-floating code block - Static, Synchronized

      Continue Reading: finalize( ) method

      Also Read: Java OOPs concepts
      Also Read: What are access specifiers and access modifiers in Java 

                       


                                                                        Java Source World               A Java quick reference blog

      Read More »

      Java Source World: Spring Interview Questions




      Spring Interview Questions:


      Popular Spring interview questions:


      Q : What is Spring ?

      A: Spring is an open source development framework for enterprise Java. T he core features of the Spring Framework can be used in developing any Java application, but there are extensions for building web applications on top of the Java EE platform. Spring framework targ ets to make J2EE development easier to use and promote good prog ramming practice by enabling a POJO-based prog ramming model.

      Q : what are benefits of using spring ?

      A: Following is the list of few of the great benefits of using Spring Framework:
      Lightweight: Spring is lightweight when it comes to size and transparency. T he basic version of spring framework is around 2MB.

      Inversion of control (IO C): Loose coupling is achieved in spring using the technique Inversion of Control. The objects g ive their dependencies instead of creating or looking for dependent objects. 

      Aspect oriented (AOP): Spring supports Aspect oriented programming and enables cohesive development by separating application business log ic from system services.
      Container: Spring contains and manages the life cycle and configuration of application objects.

      MVC Framework: Spring 's web framework is a well-designed web MVC framework, which provides a great alternative to web frameworks such as Struts or other over eng ineered or less popular web frameworks.

      Transaction Management: Spring provides a consistent transaction management interface that can scale down to a local transaction (using a sing le database, for example) and scale up to g lobal transactions (using JT A, for example).

      Exception Handling : Spring provides a convenient API to translate technology-specific exceptions (thrown by JDBC, Hibernate, or JDO, for example) into consistent, unchecked exceptions.

      Q : What are the different modules in Spring framework?

      A: Following are the modules of the Spring framework:
      1. Core module
      2. Bean module
      3. Context module
      4. Expression Lang uag e module
      5. JDBC module
      6. ORM module
      7. OXM module
      8. Java Messaging Service(JMS) module
      9. Transaction module
      10. Web module
      11. Web-Servlet module
      12. Web-Struts module
      13. Web-Portlet module

      Q : What is Spring configuration file?

      A: Spring configuration file is an XML file. T his file contains the classes information and describes how these classes are config ured and introduced to each other.

      Q : What is Dependency Injection?
      A: Inversion of Control (IoC) is a general concept, and it can be expressed in many different ways and Dependency Injection is merely one concrete example of Inversion of Control.
      This concept says that you do not create your objects but describe how they should be created. You don't directly connect your components and services tog ether in code but describe which services are needed by which components in a configuration file. A container (the IOC container) is then responsible for hooking it all up.

      Q : What are the different types of IoC (dependency injection)?
      A: Types of IoC are:

      Construc tor-based dependenc y injec tion: Constructor-based DI is accomplished when the container invokes a class constructor with a number of arg uments, each representing a dependency on other class. Read more: Constructor Injection

      Setter-based dependenc y injec tion: Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no-argument constructor or no-arg ument static factory method to instantiate your bean.
      Read more: Setter Injection

      Q : Which DI would you suggest Construc tor-based or setter-based DI?
      A: Since you can mix both, Constructor- and Setter-based DI, it is a g ood rule of thumb to use constructor arg uments for mandatory dependencies and setters for optional dependencies. Note that the use of a @Required annotation on a setter can be used to make setters required dependencies.

      Q : What are the benefits of IOC?
      A: The main benefits of IOC or dependency injection are:
      It minimizes the amount of code in your application.
      It makes your application easy to test as it doesn't require any singletons or JNDI lookup mechanisms in your unit test cases.
      Loose coupling is promoted with minimal effort and least intrusive mechanism.
      IOC containers support eag er instantiation and lazy loading of services.
      Read More »

      Tuesday 28 April 2015

      Java Source World: Spring Framework - Constructor Injection with Collection Example

      Constructor Injection with Collection Example






       Spring Framework - Constructor Injection with Collection Example


      We can inject collection values by constructor in spring framework. There can be used three elements inside the constructor-arg element.
      It can be:

      1.   list
      2.   set
      3.   map

      Each collection can have string based and non-string based values.

      In this example, we are taking the example of Forum where One question can have multiple answers. There are three pages:

          Question.java
          applicationContext.xml
          Test.java

      In this example, we are using list that can have duplicate elements, you may use set that have only unique elements. But, you need to change list to set in the applicationContext.xml file and List to Set in the Question.java file.
      Question.java

      This class contains three properties, two constructors and displayInfo() method that prints the information. Here, we are using List to contain the multiple answers.

          package com.springexample; 

          import java.util.Iterator; 
          import java.util.List; 
           
          public class Question { 
          private int id; 
          private String name; 
          private List<String> answers; 
         public Question() {} 
          public Question(int id, String name, List<String> answers) { 
              super(); 
              this.id = id; 
              this.name = name; 
              this.answers = answers; 
          }       
          public void displayInfo(){ 
              System.out.println(id+" "+name); 
              System.out.println("answers are:"); 
              Iterator<String> itr=answers.iterator(); 
              while(itr.hasNext()){ 
                  System.out.println(itr.next()); 
              }     } 
      applicationContext.xml

      The list element of constructor-arg is used here to define the list.

          <?xml version="1.0" encoding="UTF-8"?> 
          <beans 
              xmlns="http://www.springframework.org/schema/beans" 
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
              xmlns:p="http://www.springframework.org/schema/p" 
              xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
           
          <bean id="q" class="com.springexample.Question"> 
          <constructor-arg value="111"></constructor-arg> 
          <constructor-arg value="What is java?"></constructor-arg> 
          <constructor-arg> 
          <list> 
          <value>Java is a programming language</value> 
          <value>Java is a Platform</value> 
          <value>Java is an Island of Indonasia</value> 
          </list> 
          </constructor-arg> 
          </bean>    
          </beans> 

      Test.java
      This class gets the bean from the applicationContext.xml file and calls the displayInfo method.

          package com.springexample; 

          import org.springframework.beans.factory.BeanFactory; 
          import org.springframework.beans.factory.xml.XmlBeanFactory; 
          import org.springframework.core.io.ClassPathResource; 
          import org.springframework.core.io.Resource; 
          

          public class Test { 
          public static void main(String[] args) { 
              Resource r=new ClassPathResource("applicationContext.xml"); 
              BeanFactory factory=new XmlBeanFactory(r); 
               
              Question q=(Question)factory.getBean("q"); 
              q.displayInfo(); 
               
          } 
          }  

      Read More »