Object creation syntax in Java

9.3k Views Asked by At

Regarding syntax for object creation using new keyword. I know that syntax to create an object is:

Foo ref = new Foo();

Do java language in itself provide some mechanism that can create object other than standard syntax as mentioned above?

I have knowledge that Scala has such functionality or may be some other JVM compatiable language but I want to know from just core java point of view.

Without using keyword 'new' and assignment operatior '='. For example it can be like Foo ref Create(). ? Now I want to develop 'Create' method to replace traditional syntax.

4

There are 4 best solutions below

1
On

Another way you can create an object is by reflection:

Foo ref = Foo.class.newInstance()
4
On

Class.newInstance() using reflection API eg.

Foo ref = Foo.class.newInstance();

Please read the doc on Class API. Go for Spring Dependency injection, it will help you.

This page can give you a good start

5
On

Here is what you are looking for:

Different ways to create objects in Java

This is a trivia. Yeah, it’s a bit tricky question and people often get confused. I had searched a lot to get all my doubts cleared.

There are four different ways (I really don’t know is there a fifth way to do this) to create objects in java:

  1. Using new keyword This is the most common way to create an object in java. I read somewhere that almost 99% of objects are created in this way.

MyObject object = new MyObject();

  1. Using Class.forName() If we know the name of the class & if it has a public default constructor we can create an object in this way.

MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();

  1. Using clone() The clone() can be used to create a copy of an existing object.

MyObject anotherObject = new MyObject(); MyObject object = anotherObject.clone();

  1. Using object deserialization Object deserialization is nothing but creating an object from its serialized form.

ObjectInputStream inStream = new ObjectInputStream(anInputStream ); MyObject object = (MyObject) inStream.readObject();

Now you know how to create an object. But its advised to create objects only when it is necessary to do so.


Source : http://javabeanz.wordpress.com/2007/09/13/different-ways-to-create-objects/

P.S: I copied the text from the post, so that it is visible even if the link goes down.
I hope this helps.

0
On

You can avoid using the new operator in most of your code by creating a static factory method on your class:

class Foo
{
    public static Foo create()
    {
        return new Foo();
    }
}

Then instantiation looks like this:

Foo f = Foo.create();

This is not much more concise, however it does have several advantages as detailed in section 1 of Effective Java: it allows type inference of generic types (Map<String,String> m = Maps.newHashMap(); instead of Map<String,String> m = new HashMap<String,String>();), and allows you to change the method in the future to e.g. memoize the objects instead of creating a new object on each call.