File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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
Author

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.

regards
John
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 variable...like test.name("John");
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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
Bartender

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
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Printing a value returned by a method.