Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

creating an object

 
nsk nsk
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai this is sashi,

in how many ways we can create an object and how?
iam waiting for your reply.
bye.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
umm

int A = 0;

int B = 2;

int C = A + B;

there are many ways...

you can say...

int A = 0;

then later on in the program make it equal another integer.

double f = 2.3;

int z = (int)f; //type casting

String hello = "hello";

if its a generic you can do:

ArrayList A = new <Integer>ArrayList;

umm..

like i said, "creating objects" is quite a vague topic..

Justin
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15494
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
like i said, "creating objects" is quite a vague topic..

Yes, and unfortunately the examples you give don't have anything to do with creating objects. Primitives like int and double are not objects in Java, type casting doesn't have anything to do with creating objects and the syntax for creating a new ArrayList of Integer objects you gave is wrong.

You create an object by creating the constructor of a class, using the 'new' keyword.

Another way to create an object is to deserialize a previously serialized object.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My immutable rule of Java is that objects are always created using the new keyword, except in the cases where you don't have to use the new keyword.

So, anywhere you see Object o = new Object(); an object is being created.

Of course, there are various types of constructors, and there are some special objects that don't need the new keyword, like String:

String s = "s";

Although String and array objects are slight exeptions to the rule.

I'd say that's a good place to start: objects are created using the new keyword.

-Cameron McKenzie
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

If you are new to Java, then please read Introduction to Objects.
[ October 17, 2006: Message edited by: Chetan Parekh ]
 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is only way to CREATE objects... using new operator followed by constructor-call.
is there any else?
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Akhilesh Trivedi:
There is only way to CREATE objects... using new operator followed by constructor-call.
is there any else?


See Cameron's reply...
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Browse through the JavaDoc for Class and see if you spot one more.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic