Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Java Strings

Apparently, I have been studying java for past year; however, i have never encountered a code like this.

OkHttpClient client = new OkHttpClient();

String run(String url) throws IOException { Request request = new Request.Builder() .url(url) .build();

Response response = client.newCall(request).execute(); return response.body().string(); }

I don't seem to understand whats going on after String run...I want to say thats some function, but then i'm pretty sure its not. Moreover, i thought of it as a creation of a String variable, but i'm not sure. Can anyone explain to me what this code is or whats its doing?

3 Answers

Craig Dennis
Craig Dennis
Treehouse Teacher

Yeah that is just a method definition using the default access level (y'know like private/public which is protected).

It is a method called run that returns a String

Radim Štěpaník
Radim Štěpaník
157 Points

It is function which returns string value. It is similar to write

//probably global prom
OkHttpClient client = new OkHttpClient();

//function which must return String value, "throws IOException" means that IOException can be thrown by running this code, more you can read here http://stackoverflow.com/questions/11589302/why-is-throws-exception-necessary-when-calling-a-function
protected String run(String url) throws IOException {
   // creation new request to url
     Request request = new Request.Builder() .url(url) .build();
   // execute request and catch response
     Response response = client.newCall(request).execute(); 
  // return string value of response body 
     return response.body().string(); }

Thank you! :)