permaculture playing cards*
The moose likes Beginning Java and the fly likes Creating instance of an object. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating instance of an object." Watch "Creating instance of an object." New topic
Author

Creating instance of an object.

Gabriel Allen
Greenhorn

Joined: Jan 09, 2007
Posts: 12
If I want to call an object, I have to create an instance of it. So, what are the conventions? For example, if I have the following:



If I call if from another class, examples tell me that this is the proper syntax:


So, does the 1st "DoThis" have to equal the name of the class "DoThis"? The "dt" portion is anything I want it to be, correct?
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Yes, that is correct. The syntax "new DoThis()" is used to create a reference to the object 'DoThis'.

The syntax "DoThis dt" means you are declaring 'dt' do be of type "DoThis".
Gabriel Allen
Greenhorn

Joined: Jan 09, 2007
Posts: 12
Thanks. But, does the "DoThis" in the "DoThis dt" have to be the name of the object being created: "new DoThis"?

So, I can't say:"ThisThing dt = new DoThis()"; right?
[ January 12, 2007: Message edited by: Gabriel Allen ]
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
You can say 'ThisThing dt = new DoThis()' provided 'DoThis()' is a subclass of 'ThisThing'.
That is you can say 'Car ct = new Honda()' or 'Car ct = new Ford()' because Honda and Ford are types of cars or are a sub class of cars.
'Crazy cz = new President()' you figure it out
Gabriel Allen
Greenhorn

Joined: Jan 09, 2007
Posts: 12
Thanks! So, there has to be some kind of association, either class name or subclass name, that you have to declare as the object type?

Ah! Now is see! If the object is "DoThis", the object type has to be "DoThis"? So, we are infact saying,"int newNumber = new int()". Is that right?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Gabriel Allen:
Thanks! So, there has to be some kind of association, either class name or subclass name, that you have to declare as the object type? ...

Yes.

Variable name

A variable name can be any legal identifier. But whenever you declare a variable, you also need to specify its type, which is a class or interface name.

Type variableName;


Object creation

When you create a new instance, you use the keyword "new" followed by a constructor call. Constructor names match the class name exactly, and this denotes the true type of the object.

new Type();


Assignment

When you assign an object reference to a variable, the object type must be consistent with the variable type. With inheritance, a subtype IS-A supertype. (For example, if Cat extends Animal, then a Cat IS-AN Animal.) So, as Abdulla pointed out, you can have...

SuperType variableName = new SubType();


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Gabriel Allen:
..."int newNumber = new int()". Is that right?

The idea is right, but int is a primitive type -- not an object. So instead of creating a new object by calling a constructor, you simply assign a value.

int i = 7;

However, there is a wrapper type that represents an int value as an object. This is called Integer...

Integer in = new Integer(7);

(And then there's a feature called "boxing," but we won't confuse matters with that here.)
[ January 12, 2007: Message edited by: marc weber ]
Gabriel Allen
Greenhorn

Joined: Jan 09, 2007
Posts: 12
Thanks! Now I got! Or so I think.

Sorry for the "int" example. I was just trying use it as an example in the sense it is a specific type and you have to assign variables a type. Poor analogy, but I think I got the idea across. Ha!

Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating instance of an object.
 
Similar Threads
Polymorphism
Why doesn't polymorphism determine the output in this case?
Desicion pattern or smt ...
Need help on deadly diamond of death..
Dynamic Polymorphism: new method in subclass