Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Different ways to create objects

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 340
    Comment on it

    Different ways to create object:

    We can create objects in java in four different ways. 

    • Using new keyword
    • Using Class.forName()
    • Using clone()
    • Using object deserialization

    Creating an object with the help of new keyword:

    Mostly we use new keyword to create objects in java. 

    Syntax:

    MyObj obj=new MyObj();

    Creating an object with the help of Class.forName():

    Class.forName is used to load the class in Java but it do not create any Object. To Create an Object of the Class we  use "newInstance" method of Class class.

    Syntax:

    Myobj obj=(MyObj) class.forName("object").newInstance();

    Creating an object with the help of clone():

    It is used to create a copy of an existing object.

    Syntax:

    MyObj obj1=new MyObj();
    MyObj obj2=(MyObj )obj1.clone();

    Creating an object with the help of object deserialization:

    It creates an object from its serialized form.

    Syntax:

    ObjectInputStream inStr = new ObjectInputStream(anInputStream ); 
    MyObj object = (MyObj) inStr.readObject();

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: