Solved

how to create an object without using the new operator?

Posted on 2004-03-22
21
176 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

759 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now