Solved

how to create an object without using the new operator?

Posted on 2004-03-22
21
180 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

856 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