Want this course?
Fields and Methods
Course: JVM Fundamentals for Clojure
Everything in Clojure is based on Java Objects. That means it all boils down to methods and fields on objects and classes. When you're interfacing with a Java library, including the standard library, you will need to access methods and fields all the time. This lesson covers the normal and most common ways of calling methods and accessing/changing the state of objects.
Fields and methods on Java objects. Java objects have fields, which are where you store your state, and they have methods, which are like functions that return a value. Because we're on the JVM, we need a way to be able to access those.
This is the principle way that you'll be interacting with objects from the JVM. So to do a method, you just put a dot and then the name of the method. So this will be equivalent to X dot two string like that. That's the Java. Put this like that. Okay, so this is calling the two string method on X. Easy. Now if you have multiple arguments, you would put them after the X. So the X always has to go first, and so I'm gonna make up a add vector two D, so we have this vector two D here, and we're gonna add another vector.
So this is equivalent to V dot add vector two D V two. Now there's fields, too. Closure on the JVM is smart enough to distinguish between the two. It will prefer methods, but if there is no method by that name with zero arguments like this one, then it'll look to see if there's a fields with that name because fields don't take arguments. So if you had a point, you could do like this, and it'll look to see if it has a point on X method. So it'll be like point dot X, and if that doesn't work, it will try, let's put or, or point dot X, which is the field. Now there's a problem with that because now it's ambiguous, and you want to be explicit.
All right, so now there's also methods on the class themselves, the classes themselves these are called static methods in Java, and they don't belong to any particular instance, and we'll often have to use them. So there's a class called system, and this one has stuff like the standard in, standard out. You can get the time from it. In fact, if you want to get, call them the method, current time millis, you can do like this. I'll run that, and this is equivalent to system dot current time millis, millis like that. And there's really no way in Java to know that this is a static cull because this could be a variable culled system, but in closure, we used the slash, and so you know, you can see right away that this is a static method, and it's kind of nice.
Now how do you instantiate an object? You have a class, you want to instantiate it. There's two ways. The preferred way, I mean the way I like to do it, is put a dot at the end, and then of course you have arguments to your constructor, but this one doesn't have arguments. So this is just culling. This is, oh let me write the Java, Java new Java dot util dot date, like that. And if you want to give an arguments, you just put them after that. Date and system current time millis. There's a constructor that takes along, which is the unix timestamp, which is this. So now we see that that's basically now. And this is gonna be like new Java dot util dot date system dot current time millis. That's the equivalent java.
The other way to do it is to do new java dot util dot date. No dot at the end. And that does the same thing. It returns now because there's no argument. Cool. Now as we had before, we had these fields, this X. That will get the value of the field and return it, but you might want to set it, so there's a thing called set, and then you just put it right in there, and then you give it the value. So we're gonna set the X, the X coordinate to 100. Oh, and it returns, it returns the value 100 just like you would in java. Let's do java. Java point dot X equals 100. Cool.
The last thing is that Java has a thing called enums, and it's not clear, but on the JVM, enums are actually a class with a bunch of static, static fields on them. Oh, and, you know, if you have the static fields, you would access it this way too. So there's a class called Java as an enum, java dot nio dot charset dot standard charsets. Yay.
So if you want to, if you want to access the charsets, you would put a slash and the one you probably want most of the time is UTF 8. Now it is gonna ask, it's some objects that depends on what JVM you're running on, whatever, but this thing will always return the thing you want for your particular JVM. Right, so this is an enum, but it gets compiled to a regular java class with different static accessors on it. And notice I didn't put it in parentheses. That will work, too, obviously. Let's run it again. So in parentheses it works, but you don't need them because it's a field not a method, so you don't really need to cull it. So that's static field. Also works for enums.