Java classes are used to write Java code. In this video, learn how to create Java classes in Eclipse, what Java packages are, the main method, and how to modify the fonts used in the Eclipse editor.
- Now let's look at actually creating a Java file. So that we can write some code. So I'm here in my first Java project. I'm going to select my source folder, which is called SRC. And I'm going to create a new Java class to write my code. To do that, I'm going to click on this new class button. And when you click that button, you need to give your class a name. I'll call this example. And then once you type a class name, you're going to get this little message at the top here that says use of the default package is discouraged. So when you develop for Java, you want to give your classes a package. A package is like a unique identifier that's associated with your classes. This prevents conflicts when you share your code with other developers. For example, if another developer has a class called example and I want to share my code with him, if I have the default package and the other developer is using the default package, there's going to be a conflict. So what we do to prevent that, is give our packages unique names so that those conflicts don't happen. Then we can use the same name of classes within a different package so that when you're compiling your application, that it knows which class you're referring to. So, to give it a unique identifier, the common practice for the unique package name is a reverse domain structure. So if you own a domain, take the name of the domain and then com or net or whatever is the end of your domain and reverse those. So since I own toddperkins.com, I would use the package name com.toddperkins. So you reverse those two for your package name and that gives you a unique identifier, since, in theory, nobody else is going to be using that same package name. So all of my classes will be unique within my package. So once you have that set up, and of course if you don't own any domain, you can leave that blank and that's okay for what we're doing here. Package names will not be required. Once you have that set up, check this box to create the method step public static void main. This is the main entry point for your application. So when your app launches, this method is going to be executed automatically and the code within there is going to run. So once you have all that set up, hit finish, and then you should see your Java class on the screen. You can see it's within my package right there, com.toddperkins. So there's my code file I just created. And since this font is a little bit small, I'm going to bump it up through Eclipse preferences. So underneath general, appearance, colors and fonts, I'm going to expand Java. Of course you don't have to do this. I'm just doing this so it's easier for you to see. I'll select Java editor text font, edit it. I'm going to change this to Monaco 18. Close that. Hit apply and close. And the font is a little bit bigger. Again that's not a necessary step for you to do. Just something for you to see the font on my screen. So let's just write one line of code to confirm that Java code is running within Eclipse. So let's use system + dot + out. You can use the arrow keys on your keyboard to highlight the method you want to use. Press return or enter to print it out. And then after out, type a dot, and then we want print line. Just print LN for short. And you want the one that receives a string, but any one should do. So just choose that, type a string in there. I'll just put hello world. Once you've done that, you can either save the application. So you see the asterisks there indicating the file has not been saved. You can save it with a control or command + S, or you can simply run the application using the run button. And you'll be asked if you want to save the file before launching the application. That's what I'm going to do. I actually get to check this box to always save resources before launching. And then I'll hit okay. File is then saved. And then we see that output message in the console. If you don't like seeing it from the right side of the screen, simply hit this restore button right here to restore this menu to the bottom of the screen, and there it is. So we've successfully created a Java class and we've added some code to it and the code is running properly.
Released
3/21/2019- Installing Java
- Choosing an IDE
- Creating a Java class
- Java language basics
- Creating a UI with Swing
- Storing data in tables and arrays
- Publishing and using JAR files
- Creating a JPS project
- Writing server-side Java
- Developing Android apps with Java
Share this video
Embed this video
Video: Create a Java class