Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Win a copy of
Node.js in Action
this week in the
Java in General
How many ways can we create an object
Joined: Dec 08, 2006
Dec 27, 2006 05:27:00
How many ways can we create an object. To my knowledge
1) using new operator
2) using Class.forName(ClassName);
3) using clone method
are these correct, any one I missedout ?
SCJP 1.4,<br />SCWCD 1.4
Joined: Feb 24, 2006
Dec 27, 2006 05:58:00
That just loads the class dont it ? You never get a reference to a usable object in the heap. That is... you get a class descriptor that just gives you info about this class.
x = "hello"; ? Objects are created in the pool.
SCJP 6 articles
SCJP 5/6 mock exams
More SCJP Mocks
Cameron Wallace McKenzie
author and cow tipper
Joined: Aug 26, 2006
Dec 27, 2006 06:40:00
Could you say, let the JVM create one for you? Isn't that what happens with Java 1.5 autoboxing?
Vector v = new Vector(); int i =10; /*primitive type*/ v.add(i); /*autoboxing creates a new, big I, Integer*/
Similary, the new keyword doesn't need to be used with String objects, or when creating arrays. Does that count?
Isn't 'using a clone method' the same as say, using a 'factory' method? After all, isn't the JVM just using the new keyword behind the scenes? Philisophical argument I guess.
This is stated purely out of ignorance of the API, but can you do something funky with reflection as well? Just a thought.
Joined: Jan 29, 2003
Dec 27, 2006 08:42:00
Reading from an object stream might count as another way.
Yes, reflection is another option. class.forName().newInstance() only invokes no-args constructors. For other constructors you have to dig deeper into reflection.
Did we just do some homework?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
subject: How many ways can we create an object
creating an object
Ways to create an object?
How many ways can we create an object?
In how many ways we can create an object? could you Explain with example if possible.
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2014