Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Create object Without new operator

 
Manoj Paul
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my program.
An object 'object' is created using the new operator.
I want to create an object without using the new operator.
Can you suggest how?

****************************************

code:


import java.io.*;

public class AQuestion7
{
String name="Manoj Kumar Paul";


public void check()
{
System.out.println("Substring = "+name.substring(0));
System.out.println("Substring = "+name.substring(9));
System.out.println("Substring = "+name.substring(0,5));

name=name.replace('a','s');
System.out.println("My name is = "+name);
}

public static void main(String args[])
{
AQuestion7 object = new AQuestion7();
object.check();
}
}


****************************************
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can you reflection to create objects without the new operator.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
substring returns a new Object
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not an advanced question. Moving...
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Manoj Paul:
String name="Manoj Kumar Paul";
That's one way of doing it; the String literal is an object in its own right.
So would "Integer i = 123;" using autoboxing (Java 5 and 6 only).


As David O'Meara told you, a lot of the String class methods create a new object.
You can create arrays with an initialiser:-
int[] myArray = {1, 2, 3, 4, 5};

The commonest way, as suggested by Rahul Bhattacharjee, is to use a factory method. One way is to use the Class.forName("foo.bar.BazClass").newInstance() syntax, but I think it needs a no-argument constructor to work properly.

There are probably others; this question has come up before on these boards. Try a search for it.
 
Ramu Valivarthi
Ranch Hand
Posts: 37
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can create object without using 'new'.in several ways.

1:using factory methods.
2:using cloneable
3:using newInstance().
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ramu Valivarthi:
you can create object without using 'new'.in several ways.
1:using factory methods.


what is it ?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apparently continued here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic