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 trialMatthew Francis
6,967 PointsResources and toExternalForm();
For example, when we want to implement a new font type, you would usually do this:
Font.loadFont(getClass().getResource("/fonts/VarelaRound-Regular.ttf").toExternalForm(), 10);
My understanding:
getClass() = returns the class name of getResource() , so it would be java.class.resource
getResource() = gets the non-java file(a resource) from the resource folder.
toExternamForm() = returns a file url (is that: /fonts/VarelaRound-Regular.ttf?), what is the purpose of returning a file url?
1 Answer
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsI'm not sure that is 100% right, but let me try to explain this how I see it:
-
getClass()
returns "blueprint" of the class you are into, so for example, if your code is insideMain
class, it will return "blueprint" ofMain
and NOT "...class name of getResource...":
public class Main () {
public void start() {
getClass();
// equivalent to
this.getClass();
// return "blueprint" of "Main" class
}
}
For more on getClass()
read here:
http://stackoverflow.com/questions/32919106/how-does-getclass-in-java-work
getResource(String name)
is used to load resources starting from this Class location. I think better name for this method would begetREsourceUrlFromClassLocation
. So program is looking for resource file, starting from specified Class location, and returnsjava.net.URL
class.toExternalForm()
returns String representation ofjava.net.URL
object:
http://docs.oracle.com/javase/6/docs/api/java/net/URL.html#toExternalForm()
So overall why it is needed and why we do that...
Our goal is to load resource file into Font class.
In order to achieve that we need to provide correct path for resource file using which this Font can be loaded (in this case tff
file)
Now we don't have web application, so we have to play with relative paths of where our classes situated.
That's why we say
- get class where we run method
- get URL of resource relatively (from the class that was get)
- convert URL to string in order to pass argument to
Font.load
Matthew Francis
6,967 PointsMatthew Francis
6,967 PointsThank you so much for this! concise answer as always, but I just want to double check on this:
When we get the url it would return the file locaiton (eg; java/file/location) and toExternalForm() turn it into a string (eg; "java/file/location"), corect?
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsAlexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points"When we get the url it would return the file location (eg; java/file/location)" - we get instance of the
java.net.URL
class, and for some reason, not quite understandable to me, instead of justtoString()
, we usetoExternalForm()
. Although as I just looked to documentationtoString()
andtoExternalForm()
are actually exactly the same:http://docs.oracle.com/javase/6/docs/api/java/net/URL.html#toString()
http://docs.oracle.com/javase/6/docs/api/java/net/URL.html#toExternalForm()