• Post Reply Bookmark Topic Watch Topic
  • New Topic

how many ways to create an object in java  RSS feed

 
rajesh villuri
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how many ways to create an object in java ? , and also refer me a book about this topic
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is always only one way to create an object if you literally meant "create and object" which is always using the new keyword. If you are starting with Java, refer the "Head First Java"
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what about reflection and deserializing an object? Are they ultimately call new anyway?
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:And what about reflection and deserializing an object? Are they ultimately call new anyway?


I'm not sure if there would be any byte code enhancements when you use reflection or class-loaders to create an object.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite. A String literal constitutes an object. So does a boxed number. Or an array initialiser (at least I think it does).
int[] numbers = {1, 2, 3, 4, 5};
String name = "Campbell";
Integer i = 123;
There is also a method via reflectionI shall leave you to work out why the last example will fail at run‑time.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!