File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Murach's Java Servlets and JSP this week in the Servlets 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: 121
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: 186
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: 18541

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: 557
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:

Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37926
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!
It is sorta covered in the JavaRanch Style Guide.
subject: Printing a value returned by a method.
Similar Threads
i dun understand....
static variable remains unchanged
Doubt in InnerClass
String immutability
Referencing classes in other files.