Prepare for your Next Interview
|
Welcome to the Geeks Talk forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
This is a discussion on Object without using 'new' within the Java forums, part of the Software Development category; Can we create an object with out using 'new' keyword in java?...
|
|||||||
|
|||
|
Re: Object without using 'new'
yeah in case of Strings we can create a new object by using the below given statement
String str = "Test_String"; this will create a new String object and the reference variable str will refer to that object. the value of that object will be Test_String |
| The Following User Says Thank You to akhilsyal1982 For This Useful Post: | ||
|
|||
|
Re: Object without using 'new'
ya this is only possible with strings.
String is a class in Java. You can create java String object using new. String str=new String(""); String str="erer"; Both ways create String object. Hope iam clear
__________________
:) NEVER SAY DIE. |
|
|||
|
Re: Object without using 'new'
Quote:
1.clone() 2.newinstance() in java.lang.class |
|
|||
|
Re: Object without using 'new'
Yes, Thats quite possible using the factory methods or using Creational Patterns.
|
|
|||
|
Re: Object without using 'new'
Hi lavanya we can create object without using new opearator.
1) Using factory methods Ex:- NumberFormat obj=NumberFormat.getInstance(); 2)Using newInstance() method Ex:- Class c=Class.forName("SomeClassName"); SomeClassName obj=c.newInstance(); 3)using cloning Ex:- ClassName c1=new ClassName(); ClassName c2=c1.clone(); |
|
|||
|
Re: Object without using 'new'
yes,we can create the object in 3 ways without using 'new' keyword
1)by calling clone() which has present in Object super class 2)using factory methods,we can get the same type of the object Ex:take Calender class which is in java.util package,we will get the object of Calender class in following way Calender objCal = Calender.getInstance(); here getInstance() is factory method 3)using newInstance() ,we can Create a new instance of the class represented by this Class object Ex:Class objClass=Class.forName("Driverclass name");//objClass is the Class object for the class with the specified name Object obj=objClass.newInstance(); and then type cast into our class and use that object Last edited by Gangadhar123; 06-06-2007 at 01:38 AM. |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Object Spy | suji | QTP | 3 | 06-19-2009 10:05 AM |
| how to capture properties of an object | niharika38 | Rational Robot | 1 | 06-01-2007 09:14 AM |
| Cannot identify the object | Livetek | QTP | 5 | 04-30-2007 08:31 AM |
| QTP- wrong object recognised... | nusezak | QTP | 1 | 02-14-2007 04:37 AM |
| Create Object | JobHelper | Java | 2 | 11-30-2006 01:45 AM |