This tutorial will show you how to create your first Java class for a web development application. We will be using JDK and the eclipse IDE for the development so you need to have JDK and Eclipse installed on your PC.
Start Eclipse and Create New Java Project
Start Eclipse and then select the workspace you want to work in. The workspace is merely the file folder on your computer where Eclipse will save the project files. Once Eclipse has started, then you need to create a new Java project so that Eclipse can create and assemble all the project files you need for the project.
Create a new Java project select “file” and then select “new” and then select “New Java Project” from the main menu. This will open up a new project window that will allow you to name your first Java project.
For this tutorial, I have named the project “Myfirstproject”. You can then go on to choose your selection of JRE or Java Runtime Environment. For the purposes of this tutorial we will leave these settings at the default settings. Click “Finish” and Eclipse will create all the course files you need for the application.
Once you have a new program and the files you need, you need to create a new package for your project. The package naming convention follows website naming conventions so we will call our package org.myfirstproject.tutorial. Eclipse will create the package for you automatically:
In this tutorial we will create one class with a few methods and constructors. These form the backbone of any program. We will not create the actual program itself.
To learn Java programming, the Programming Java for Beginners – The Ultimate Java Tutorial course contains 115 lessons and over 10 hours of content to help you become a Java master. You will learn all about object oriented programming languages and how they work. The course will teach you about classes, objects, loops and more. The course is designed to get beginner programmers up to speed quickly and to help seasoned programmers improve their Java skills. The visual training offered by the course will help students retain information and accelerate their learning.
Creating Classes for Your Java Web Development Application
Creating classes in Eclipse is fairly simple. Right click your package in the packages folder and select “new” and then select “class”. This will open up a wizard that will allow you to create a new class. The first class we will create will be “friends” so enter friends into the name of the class and then leave the settings at default and click finish. This will create a class called “friends” for you and the code for this class will automatically open in the center panel:
Now that we have created a class, we need to create the fields within the class. The fields are the elements within the class that store the data. The field in the “friends” class will be a simple field that stores our friend’s name. Since the name will be stored as alphabetical characters the type of field is a string. To add the field simply declare a string called “fname” in the class. Here is the code to do that:
It is important to remember that all names are case sensitive in Java so you need to make sure you bear that in mind when naming the classes and fields.
Now that we have a class, we need to add a constructor to initialize the class. To create the constructor to initialize the class all we need to do is create an initial value for the variable we created:
Pay attention to the editor. Eclipse automatically checks for formatting and coding errors. If you click on the error on the left hand side of the code, then Eclipse will offers various solutions to fix the problems within your code.
If you would like to learn more about Java classes and objects, or how to define variables in Java, Java 101: Java Programming for Beginners offers a superb introduction to Java programming for beginners. The course starts off by teaching you what object oriented programming is and then moves onto basic Java syntax. The course contains 37 lessons that will teach you how to setup the java development environment properly. You will then learn how to work with various variables including integers, floating point variables, math functions and strings. The course will teach you what simple and complex conditionals are and how to create them and it will show you how to create while, do while and for loops. You will also learn to work with standard and multidimensional arrays. The course will show you how to code arrayList objects and then it will teach you how to create simple and custom functions for your programs.
Add a Method to the Class
Now that we have a class, we need to add some methods to the class. For this tutorial we will add one simple “getter” and “setter” method for the class. The getter gets data from the class and the setter element allows you to set information for the class, for example, if you have user input and want to save that input to your class. We will use an extremely simple set of code to get the name and set the name. Take a look at the code needed below:
The getFname will return the friends name and the setFname method will set the name given equal to fname. Note that the set name element contains parameters within the brackets and the get name does not require parameters. We now have a complete class that contains fields, constructors and methods and we are ready to use this class.
To learn more about classes and parameters, sign up for the Java Fundamentals I and II course now. This course has 149 lectures that will teach you the core Java principles including how to use classes, objects and methods in your programs. It covers the key OOP techniques and how inheritance and polymorphism works. It also includes Java concepts such as exception handling and user interface design.
Use Scrapbook to Create Code
You can use the scrapbook feature within Eclipse to write and test your Java code or you can use the JUnit test mode. This feature is great for writing and testing code at the same time. To create a scrapbook page for your project, select the project from the main menu, select “new”, “java” and then “Other”. Select “Java Run/Debug” and select scrapbook page. A new scrapbook page is added to your project:
You can test elements and code within the scrapbook and JUnit test pages to ensure your code is working properly as you develop your program.
Now that you have a class with methods, you have the basics for a program. For more training on Java development, sign up for the Introduction to Java Training Course now. This course offers 205 lectures and over 20 hours of content to help you really take your Java programming skills to the next level. You will learn to write standalone applications in Java using the Eclipse IDE. You will learn all about object oriented programming concepts and how they work. The course will teach you about Java classes, objects and other features and you will learn how to create well scoped classes using packages. You will learn to write error handling exceptions for your programs to ensure they run smoothly without errors. You will learn to read and write data output streams and you will learn to use Java collections framework to work with groups of objects.