Results 1 to 8 of 8

Thread: Object without using 'new'

  1. #1
    Junior Member
    Join Date
    Nov 2005
    Answers
    2

    Object without using 'new'

    Can we create an object with out using 'new' keyword in java?


  2. #2
    Junior Member
    Join Date
    May 2007
    Answers
    3

    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


  3. #3
    Expert Member
    Join Date
    Oct 2005
    Answers
    383

    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.

  4. #4
    Junior Member
    Join Date
    May 2007
    Answers
    3

    Re: Object without using 'new'

    Quote Originally Posted by ani_lavanya View Post
    Can we create an object with out using 'new' keyword in java?
    Yes we can create object without using new
    1.clone()
    2.newinstance() in java.lang.class


  5. #5
    Junior Member
    Join Date
    May 2007
    Answers
    1

    Re: Object without using 'new'

    Quote Originally Posted by ani_lavanya View Post
    Can we create an object with out using 'new' keyword in java?
    Yes, Thats quite possible using the factory methods or using Creational Patterns.


  6. #6
    Junior Member
    Join Date
    Jun 2007
    Answers
    2

    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();


  7. #7
    Junior Member
    Join Date
    May 2007
    Answers
    1

    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 12:38 AM.

  8. #8
    Junior Member
    Join Date
    May 2007
    Answers
    15

    Re: Object without using 'new'

    Yes, without using New keyword u can create objects in Java

    Last edited by umeshap; 06-06-2007 at 06:26 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact