This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Java in General and the fly likes Printing a value returned by a method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Printing a value returned by a method." Watch "Printing a value returned by a method." New topic

Printing a value returned by a method.

John Paterson
Ranch Hand

Joined: Mar 12, 2012
Posts: 162
Hi Guys,

I wrote the following code just to experiment handling a value returned by a called method. Following is the code:

My objective is to print from the main method, the value returned by the name method. How do I go about acheiving it? I get a 'cannot find symbol' error message with the above mentioned prinln statement. Hope someone can advise. Thanks.

Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 187
John Paterson wrote:I get a 'cannot find symbol' error message with the above mentioned prinln statement.

Because you have not defined an Instance variable named as "name" in the class Test, you have defined an Instance method "String name(String s){}".

So, try out to call a method...not a"John");
Paul Clapham

Joined: Oct 14, 2005
Posts: 19728

How do you do it? Well, first you assign the value returned by the method to a variable. And then, you print the value of that variable.
Greg Brannon

Joined: Oct 24, 2010
Posts: 563
In case you're having difficulty visualizing the fine answers you've been given thus far, to obtain the value returned by a method, assign it to a variable in the calling method:

That's to demonstrate how it's done using a variable, but you could also print the returned value directly:

Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 44048
I would suggest you try something which doesn’t simply return its argument.There are some very dubious classifications of methods available, and this one is the most dubious of all. That duplicate method comes out as 1368 in that classification, so I marked it static. The private constructor means you must call that method the stylistically‑correct way:What you are doing in your method is creating a local variable referring to the same object as the parameter, and returning that reference without doing anything else with it. An optimising compiler may simply reduce that method to this!
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Printing a value returned by a method.
It's not a secret anymore!