• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object constructor returns something

 
Andy Powell
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Inder Kumar Rathore
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...........



 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andy Powell
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I understand now.

Thank you all for your help
 
Andy Powell
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I understand now.

Thank you all for your help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic