Saturday 16 February 2019


How Many Ways to create Object in java?






public class NewKeywordExample
{
    String name = "JavaTechnology";
    public static void main(String[] args)
    {
        NewKeywordExample obj = new NewKeywordExample();
        System.out.println(obj.name);
    }
}







===================================================================================








public class NewInstanceExample
{
    String name = "JavaTechnology";
    public static void main(String[] args)
    {
        try
        {
            Class cls = Class.forName("NewInstanceExample");
            NewInstanceExample obj =
                    (NewInstanceExample) cls.newInstance();
            System.out.println(obj.name);
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
       }
        catch (InstantiationException e)
        {
            e.printStackTrace();
        }
        catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }
    }
}


=======================================================================================


public class CloneExample implements Cloneable
{
    @Override
    protected Object clone() throws CloneNotSupportedException
    {
        return super.clone();
    }
    String name = "JavaTechnology";
    public static void main(String[] args)
    {
        CloneExample obj1 = new CloneExample();
        try
        {
            CloneExample obj2 = (CloneExample) obj1.clone();
            System.out.println(obj2.name);
        }
        catch (CloneNotSupportedException e)
        {
            e.printStackTrace();
        }
    }
}


===============================================================================================


import java.lang.reflect.*;
public class ReflectionExample
{
    private String name;
    ReflectionExample()
    {
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public static void main(String[] args)
    {
        try
        {
            Constructor<ReflectionExample> constructor
                = ReflectionExample.class.getDeclaredConstructor();
            ReflectionExample r = constructor.newInstance();
            r.setName("JavaTechnology");
            System.out.println(r.name);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}



=============================================================================================================






import java.io.*;
class DeserializationExampleTest implements Serializable
{
    private String name;
    DeserializationExampleTest (String name)
    {
        this.name = name;
    }
    public static void main(String[] args)
    {
        try
        {
            DeserializationExampleTest d =
                    new DeserializationExampleTest("JavaTechnology");
            FileOutputStream f = new FileOutputStream("file.txt");
            ObjectOutputStream oos = new ObjectOutputStream(f);
            oos.writeObject(d);
            System.out.println("Data Write Successfully");
            oos.close();
            f.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    DeserializationExampleTest d=null;
        try
        {
            FileInputStream f = new FileInputStream("file.txt");
            ObjectInputStream ois = new ObjectInputStream(f);
            d = (DeserializationExampleTest)ois.readObject();
 
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        System.out.println(d.name);
    }



=======================================================================================================

No comments:

Post a Comment