What is difference between == and equals() in java?


== operator is used to compare the references of the objects.

public boolean equals(Object o) is the method provided by the Object class. The default implementation uses ==
operator to compare two objects.

But since the method can be overridden like for String class. equals() method can be used to compare the values of two
objects.

String str1 = new String("MyName");
String str2 = new String("MyName");

if(str1 == str2){
System.out.println(“Objects are equal”)
}else{
System.out.println(“Objects are not equal”)
}

if(str1.equals(str2)){
System.out.println(“Objects are equal”)
}else{
System.out.println(“Objects are not equal”)
}

Output:
Objects are not equal
Objects are equal

Lets look at other snippet:

String str2 = "MyName";
String str3 = str2;

if(str2 == str3){
System.out.println(“Objects are equal”)
}else{
System.out.println(“Objects are not equal”)
}

if(str3.equals(str2)){
System.out.println(“Objects are equal”)
}else{
System.out.println(“Objects are not equal”)
}

Objects are equal
Objects are equal

Can a static block throw exception?


Yes, static block can throw only Runtime exception or can use a try-catch block to catch checked exception.
Typically scenario will be if JDBC connection is created in static block and it fails then exception can be caught, logged and application can exit. If System.exit() is not done, then application may continue and next time if the class is referred JVM will throw NoClassDefFounderror since the class was not loaded by the Classloader.

how to access properties file in Spring


PropertyPlaceHolderConfigurer A property resource configurer that resolves placeholders in bean property values of context definitions. It pulls values from a properties file into bean definitions.

The default placeholder syntax follows the Ant / Log4J / JSP EL style: ‘$’

PropertyOverrideConfigurer A property resource configurer that overrides bean property values in an application context definition. It pushes values from a properties file into bean definitions
Example:
A simple class of which values are set from property file.

public class MyBean {

private String myName;
private String password;
/**
* @param args
*/
public static void main(String[] args) {

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("mybean.xml");
MyBean bean = (MyBean)ctx.getBean("myBean");
// getting the properties from the property file
System.out.println(bean.getMyName());
System.out.println(bean.getPassword());
}
public String getMyName() {
return myName;
}
public void setMyName(String myName) {
this.myName = myName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}

The xml file config

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-lazy-init="true">

<bean id="configProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>config.properties</value>
</list>
</property>
</bean>

<bean id="myBean" class="test.tetst.MyBean">
<property name="myName" value="${myname}" />
<property name="password" value="${password}" />
</bean>
</beans>

config.properties files
myname=testing
password=password

Can an Innerclass be instantiated in Spring?


Dont be surprise but the answer is yes..

package com.src;
public class MyClass{
public static class InnerClass{
}
}

In spring.xml it Innerclass can be instantiated as:

< bean id="myclass" class="com.src.MyClass$InnerClass">

Wow !! this is good , spring is amazing.The innerclass should be public and static only.
But i am wondering ,will  there be any real significance left of InnerClass or not?