Get your CodeRanch badge!*
The moose likes Beginning Java and the fly likes Object constructor returns something Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object constructor returns something" Watch "Object constructor returns something" New topic
Author

Object constructor returns something

Andy Powell
Greenhorn

Joined: Apr 18, 2010
Posts: 20
This is probably really obvious to experienced people but

if I create an object which in the constructor returns a String array then why is it that upon creating said object I can't use what is returned.

Say I have

And the object returns String[].

It says incompatible type
found: object
required: String[]

I expect its pretty basic.

Thanks in advance
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39535
    
  27
"new" is a constructor - it returns an instance of whatever class name it is applied to - "object" in this case. So it needs to be assigned to a variable of type "object" or a superclass of it.

I'm not sure what you mean by "And the object returns String[]." - that's not how constructors work.


Ping & DNS - updated with new look and Ping home screen widget
Inder Kumar Rathore
Ranch Hand

Joined: Apr 14, 2010
Posts: 41
first of all constructor does not return anything
if you try to give a return type to a constructor it becomes simple method.

class A
{
String A ()
{
return "abc";
}
}


In this case compiler generates a default constructor as there is not any definition for constructor provided by the user...........




Thanks

Living is easy with your eyes closed.....
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

As Inder says, constructors do not return anything.

A constructor is a special block of code that is called by Java to initialize a newly created object. You can't specify a return type for constructors, like you can with methods. There's no way you can write a class where the constructor returns a String[].

The new operator returns an object that's of the type that you specified, as Ulf explained.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Andy Powell
Greenhorn

Joined: Apr 18, 2010
Posts: 20
Thanks for the quick replies.
I thought that if you had a method with the same name as the class then that would be called on creation. I thought that was the constructor and if you didn't have one then it would use a default one.
Am I right in saying then that if you have a method with the same name but that returns something then it will not be called on creation and so I would have to create the object and then call the method that way?

Thank you all very much for your help
Dhan Kumar
Greenhorn

Joined: Aug 03, 2009
Posts: 29

Am I right in saying then that if you have a method with the same name but that returns something then it will not be called on creation and so I would have to create the object and then call the method that way?



---------A Perfect yes


Dhan
SCJP - Here for Knowledge..
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

Andy Powell wrote:Thanks for the quick replies.
I thought that if you had a method with the same name as the class then that would be called on creation. I thought that was the constructor and if you didn't have one then it would use a default one.

No, a constructor is not a method (although it looks a lot like one). You can make a method that has the same name as the class, but it will be just a method (and not a constructor). But don't do that, because it's really confusing.

Andy Powell wrote:Am I right in saying then that if you have a method with the same name but that returns something then it will not be called on creation and so I would have to create the object and then call the method that way?

Yes that's right, but don't do that, because it's very confusing...
Andy Powell
Greenhorn

Joined: Apr 18, 2010
Posts: 20
Ok, I understand now.

Thank you all for your help
Andy Powell
Greenhorn

Joined: Apr 18, 2010
Posts: 20
Ok, I understand now.

Thank you all for your help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object constructor returns something
 
Similar Threads
Concstructors withing constructors
Application Related
to get sting value into object
Class<?> type belongs to which class
JTree with checkboxes not getting updated question