Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
Join author David Gassner as he explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more. This course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts.
In this video course so far, I've shown you how to create two types of methods, class methods that are called from a definition of a class and instance methods that are called from an instance of the class or an object. The same distinction occurs with fields or variables. When you declare a variable as a member of a class using access modifiers such as public and a data type such as string, it's automatically an instance variable or an instance field. You can also create class fields by incorporating the additional keyword static.
The most common reason to do this is to represent a constant value. Java doesn't support an explicit const or constant keyword, so instead, you typically set constant values using a combination of static and the keyword final, which means that a value can't be changed once its been set. I'll demonstrate this in the Olive class in this version of the project class variables, and I'll define a few possible values for the color. Right now my color field has a value of 000000 which has equivalent value of black, but I want to make my code a little bit more readable.
So I'll place my cursor inside the class declaration and I'll declare a field using these keywords, public, static, final. Public means that the field would be accessible from the entire application. Static means that it's a class field, it will be accessed from the class definition, not from an instance of a class, and final means its value can't be changed. Next, I'll set the data type long, to match the field data type of color, and then the name of the constant will be set.
The convention for constant values is to set their names in all uppercase. So I'll set the name of this one as BLACK, then I'll put in the equal assignment operator and I'll copy the value from the color field. I'll finish the declaration with a semicolon and now I have a constant value to work with. Next, I'll go down to the color field where I've used the literal value. I'll get rid of that and I'll replace it with Olive.; and I'll see the value BLACK appear.
I'll Save the changes and Run the application and you should see that it runs exactly the same way it did before. Now I'll add one more field, I'll copy the one for BLACK and paste it in, I'll change the second one from BLACK to GREEN, and I'll change the hexadecimal color value to 00FF00 and you can keep on doing this, adding as many different Olive colors as you need it. I'll Save the change and Run it again and even though I'm not using that GREEN value the application still runs just fine.
So this is a look at how to declare constants or class variable. Again, there's no constant keyword in Java, instead, you declare values using the keywords static and final. You can make these fields public, private, or protected, whatever you need, and you set their names to all uppercase, and that's just a very strong convention that all Java developers follow.
There are currently no FAQs about Java Essential Training.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.