wood burning stoves 2.0*
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
Author

creating an object

nsk nsk
Greenhorn

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.
bye.
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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


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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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 7 API documentation
Scala Notes - My blog about Scala
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

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: 3636
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: 1526
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: creating an object
 
Similar Threads
connection
Object to int array conversion
nullpointerexception
Called abstract class methods from another class returns null value
Strings information required