• Post Reply Bookmark Topic Watch Topic
  • New Topic

can we have class name as return types in functions in java  RSS feed

 
archu sweet
Ranch Hand
Posts: 66
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we have class name as return types in functions in java ?



what is the use?? we don't have like this option in c++ ??
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can.

In C++, you'd probably return a pointer to a Bicycle. The Java version returns a reference to a Bicycle object, which is very similar, really.
 
Tejas Shah
Greenhorn
Posts: 23
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Yes, This returns the whole object fastest to the caller....

Something like,



and the fastest will contain reference to myBike or yourBike, whichever is returned.


One Advice: Java is different in C++ from many aspects,
Just carry on with the fundamentals of OOP and dive into Java as a new language. Otherwise it can confuse you like anything!!
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tejas Shah wrote: . . . This returns the whole object . . .
Not quite. It returns a value which is the same as the memory location of that object. It does not actually return the whole object.
One Advice: Java is different in C++ from many aspects, . . .
Very wise advice. Lots of people think Java™ and C++ are versions of the same language and get very confused about it.
 
Tejas Shah
Greenhorn
Posts: 23
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree.
Java never passes the objects, but the reference.

As there is nothing like a pointer in Java, this is just an internally handled and we can access the same object at the caller. To be precise, suppose, 'myBike' is faster, 'fastest' and and 'mybike' will refer to the same object. and changes made to either of these will be reflected in each other... it's like having two tags atached to the same things.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or two remote controls controlling the same car
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David O'Meara wrote:or two remote controls controlling the same car

I just love the idea of a remote control cat! Where can I get one?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All cats work by remote control. Only the cat does the controlling . . .
 
Tejas Shah
Greenhorn
Posts: 23
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cat... ???

It was car we ware talking about. Anyways, I'm also interested in Remote Control Cat, if exists!!

- Tejas
SCJP 6

Cherish your life..!!
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tejas Shah wrote:Cat... ???

The link David posted talks about cats.

I apologise for taking this off topic, though. I'll shut up now.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tejas Shah wrote:Cat... ???

It was car we ware talking about. . . .
But that doesn't stop us taking the discussion off-topic
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!