Friday, 12 December 2014

What does Class.forName do exactly?


Class.forName method is used to load a class dynamically.

Example
Class.forName(“Abc”);

Above statement loads class “Abc”, if it is not loaded already, and returns the Class object associated with this class/interface.

Let’s see it by an example

public class Person {
    private int id;
    private String firstName;
    private String lastName;

    static int totalEmployees;

    static{
        System.out.println("Static block is initializing");
    }

    static{
        totalEmployees = 1;
        System.out.println("Total employees set to 1");
    }
    
    public String getFirstName() {
        return firstName;
    }

    public int getId() {
        return id;
    }

    public String getLastName() {
        return lastName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

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

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

public class PersonTest {
    public static void main(String args[]){
        try {
            Class.forName("Person");
            Class.forName("Person");
        }
        catch (ClassNotFoundException ex) {

        }
    }
}


Output
Static block is initializing
Total employees set to 1


When you call “Class.forName("Person");” first time, then the class Person is dynamically loaded and initialized by the class loader.  Initialization means all static fields are set to default values, static blocks are executed in the order you specified in the class file.

So when I tries to call “Class.forName("Person");” second time, no initialization happens, since class is loaded already (No static blocks executed again).

As I said, class.forName method returns Class object associated with this class. Once you got the Class instance associated with any class, you can initialize the object, by using “newInstance” method of class “Class” like below.

Class c1 = Class.forName("Person");
Person p1 = (Person)c1.newInstance();

public class PersonTest {
    public static void main(String args[]){
        try {
            Class c1 = Class.forName("Person");
            Person p1 = (Person)c1.newInstance();

            p1.setFirstName("Hari");
            p1.setLastName("Krishna");
            p1.setId(358);

            System.out.println(p1.getId() +"." +p1.getFirstName() +" " + p1.getLastName());

        }
        catch (ClassNotFoundException e) {

        }
        catch(InstantiationException e){
            
        }
        catch(IllegalAccessException e){

        }
    }
}


Output
Static block is initializing
Total employees set to 1
358.Hari Krishna




Friday, 5 December 2014

param element


The param element is used to define a variable.

Syntax
<param name="property">value</param>

You can use param element in action, interceptor, result-type elements.

 struts.xml
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="default" extends="struts-default">
            
        <action name="numberCheck" class="NumberCheckAction">
            <param name="blog">https://self-learning-java-tutorial.blogspot.com</param>
            <result name="success">/success.jsp</result>
            <result name="failure">/failure.jsp</result>
        </action>

    </package>
</struts>


<param name="blog">https://self-learning-java-tutorial.blogspot.com</param>

Statement sets the variable blog to the value ‘https://self-learning-java-tutorial.blogspot.com’.

public class NumberCheckAction {
    private int number;
    private String blog;

    public String getBlog() {
        return blog;
    }

    public void setBlog(String blog) {
        this.blog = blog;
        System.out.println("Blog set is " + blog);
    }
    
    private static String SUCCESS = "success";
    private static String FAILURE = "failure";
    private static String ZERO = "zero";

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String execute(){
        if(getNumber() == 0)
            return ZERO;
        if(getNumber() %2 == 0){
            return SUCCESS;
        }
        else{
            return FAILURE;
        }
    }
}


Run ‘numCheck.html file, you can observe the message like ‘INFO: Blog set is https://self-learning-java-tutorial.blogspot.com’ in server log.

Prevoius                                                 Next                                                 Home