File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes creating an object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "creating an object" Watch "creating an object" New topic

creating an object

nsk nsk

Joined: Oct 16, 2006
Posts: 3
hai this is sashi,

in how many ways we can create an object and how?
iam waiting for your reply.
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802

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;


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


You down with OOP? Yeah you know me!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

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

Joined: Sep 16, 2004
Posts: 3640
Welcome to JavaRanch!

If you are new to Java, then please read Introduction to Objects.
[ October 17, 2006: Message edited by: Chetan Parekh ]

My blood is tested +ve for Java.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1599
There is only way to CREATE objects... using new operator followed by constructor-call.
is there any else?

Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
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

Joined: Jan 29, 2003
Posts: 8791
Browse through the JavaDoc for Class and see if you spot one more.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Consider Paul's rocket mass heater.
subject: creating an object
It's not a secret anymore!