1

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.

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
Moby
  • 29
  • 1
  • 3

4 Answers4

4

Another way you can create an object is by reflection:

Foo ref = Foo.class.newInstance()
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • you are right but I was thinking like someway without using keyword 'new' and assignment operatior '='. For example it can be like Foo ref Create(). – Moby Jan 26 '14 at 11:56
2

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.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
Salman Khakwani
  • 6,684
  • 7
  • 33
  • 58
  • Excuse at the bottom notwithstanding, this is still plagiarism. – Marko Topolnik Jan 26 '14 at 11:54
  • The link can die, and to make this answer independent of it. I have copied the text, with sources mentioned in it. This is clearly not plagiarism. – Salman Khakwani Jan 26 '14 at 11:57
  • Just mentioning the source is not enough: making your answer nothing but a copy-paste, with no contribution of your own, is plagiarism just as well. – Marko Topolnik Jan 26 '14 at 11:58
  • 2
    @SalmanAyub Don't worry, the answer is still here : http://stackoverflow.com/a/5104630/1587046 – Alexis C. Jan 26 '14 at 11:58
  • 1
    There's no plagiarism here; it might not be proper SO procedure, but the answer credits the author of the original and only quotes a reasonable length of it. Absent SO practice, there's nothing improper about it. – arcy Jan 26 '14 at 12:07
0

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

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
  • Thanks for reply. But I am looking or thinking about flexibility as far as object creation is concerned. Foo.Class.newInstance() still binds me to predefined syntax. I am looking more flexibility, which adheres to java syntax but gives the power to customize the object creation process at my own will. – Moby Jan 26 '14 at 12:19
  • Then I believe you are looking for Dependency injection - Object creation by the framework based on the configuration – Keerthivasan Jan 26 '14 at 12:25
  • @Octopus I doubt he is looking for DI, more like DSL's and custom syntax – om-nom-nom Jan 26 '14 at 14:17
  • @om-nom-nom yeah, you are right! may be i am wrong. From the OP, it was difficult to undestand – Keerthivasan Jan 27 '14 at 05:13
0

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.

MikeFHay
  • 8,562
  • 4
  • 31
  • 52