Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

how to create an object without using the new operator?

how to create an object without using the new operator?
0
smruti_patra
Asked:
smruti_patra
  • 4
  • 4
  • 3
  • +6
2 Solutions
 
CEHJCommented:
Object o = Class.forName("SomeClass").newInstance();
0
 
krakatoaCommented:
String myString = "Hello";
0
 
krakatoaCommented:
or you can go the clone() route if protected class;

 o = new myObject();

 k = o.clone();//this is a new object without using 'new'.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
zzynxSoftware engineerCommented:
>> how to create an object without using the new operator?
Some more explanation would be nice.
And why do you need that?
0
 
WebstormCommented:
In Java, all object variables are actually pointers :
    Object obj=null;      // null pointer
    obj=new Object();   // object allocation

You can use CEHJ and krakatoa suggestions if you don't want the new word in your source code but new is still called (directly or not).

0
 
CEHJCommented:
>>In Java, all object variables are actually pointers :

Well, only in a logical sense are they pointers. They are not real pointers of course
0
 
theJay28Commented:
There are only a few methods of creating an object without using the new operator:

1.  Reflection:
     
     Object obj = Class.forName("ClassName").newInstance();
     IllegalAccessException, ClassNotFoundException and InstantiationException need to be handled and the class has to be accessible in scope from the context you are instantiating it from.

2.  Assignment:

     If you know the value you wish to assign to the object then an object can be created with the initial assignment.

     Class varName = getClass(param);
     
    You can also assign null and later assign value to the object.

     String newString = null;
   
     if(x=y){
        newString=getValue(param);
      }

0
 
krakatoaCommented:
>> Class varName = getClass(param); ...

I'm not sure this is the same thing at all as an object of the same class; this is an object holding a reference to the class of the object.
0
 
krakatoaCommented:
By which I mean they wont pass am instanceof comparison.

Also, getClass()doesnt take any parameters.

Neither is there a method called getValue(param) that can be called on a String, and by if(x=y), you meant if(x == y).

Also, CEHJ already mentioned reflection. ;)
0
 
deanboy3Commented:
You may be referring to the factory design pattern,  This is where you have another object that creates objects for you.  
See
http://www.dofactory.com/patterns/PatternFactory.aspx
for more information.

Also, you  could just call the objects constructor directly.  

What do you have against new?
0
 
sciuriwareCommented:
I think you all forgot to emphasize that   new   calls the constructor of a class.
If you don't want to have the constructor to do its work you can use one of the methods above.
That can give rise to initialisation problems!

If you want to have control over the number of objects created (or such), it's good usage
              to create a class method (static!) like:

     public <type of the clas> createInstance()

which returns a reference to a new object or null when refused,
then set the constructor to private and call it from the "createInstance()".

Then nobody can use 'new' anymore and control is completely in the 'hands' of createInstance().

;JOOP!
0
 
zzynxSoftware engineerCommented:
Input of the author needed!
0
 
sciuriwareCommented:
Give it a day, some people can't access EE every day.
;JOOP!
0
 
zzynxSoftware engineerCommented:
Now it's a week.
;)
0
 
sciuriwareCommented:
Did you notice the huge number of orphan questions in EE? Bad manners?

;JOOP!
0
 
smruti_patraAuthor Commented:
how can i use the bundle attribute in the the message tag in the jsp
0
 
zzynxSoftware engineerCommented:
Hi,

This forum works as follows:
1) You ask a question
2) You get comments
3) You are so kind to accept one or more comments to close the solved question
4) You have a new question? Just post a *new* question in this forum.
0
 
VenabiliCommented:
smruti_patra,

As I see your initial question is answered. So please accept the answer and ask your new quetsion in a new thread. THanks

Venabili
Java Page Editor
0
 
theJay28Commented:
Regarding krakatoa's answer of using the Object.clone(),
It's actually not the same as calling the new operator as this operation returns a shallow copy of the object and not a new instance.  See the following sun documentation:

http://java.sun.com/j2se/1.3/docs/api/java/lang/Object.html#clone()
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
  • 3
  • +6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now