Solved

how to create an object without using the new operator?

Posted on 2004-03-22
21
178 Views
Last Modified: 2010-03-31
how to create an object without using the new operator?
0
Comment
Question by:smruti_patra
  • 4
  • 4
  • 3
  • +6
21 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 63 total points
ID: 10648906
Object o = Class.forName("SomeClass").newInstance();
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 10648915
String myString = "Hello";
0
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 62 total points
ID: 10649098
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 37

Expert Comment

by:zzynx
ID: 10649122
>> how to create an object without using the new operator?
Some more explanation would be nice.
And why do you need that?
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10649188
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10649230
>>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
 

Expert Comment

by:theJay28
ID: 10649324
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
 
LVL 16

Expert Comment

by:krakatoa
ID: 10649445
>> 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
 
LVL 16

Expert Comment

by:krakatoa
ID: 10649503
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
 

Expert Comment

by:deanboy3
ID: 10652249
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
 
LVL 24

Expert Comment

by:sciuriware
ID: 10655986
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10656007
Input of the author needed!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 10656191
Give it a day, some people can't access EE every day.
;JOOP!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10707901
Now it's a week.
;)
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 10710846
Did you notice the huge number of orphan questions in EE? Bad manners?

;JOOP!
0
 

Author Comment

by:smruti_patra
ID: 10831887
how can i use the bundle attribute in the the message tag in the jsp
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10831981
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
 
LVL 20

Expert Comment

by:Venabili
ID: 10860263
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
 

Expert Comment

by:theJay28
ID: 11676027
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

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySQL  on Tomcat 8 46
hibernate example issues from command prompt 10 42
Java string replace 11 48
jsp login check 12 32
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question