Step 2

Creating a new class “WordChecker”


For our first bit of code, let’s practice creating a new class of our own.

On the left side of your window, right click on the “com.brainstormedu.JavaIntro” folder. Select “New->Java Class”.

Name the new class “WordChecker”. This is going to be a class that will be given some text and tell us if the string it was given is small, normal, or big.

Creating a variable and a function


Next, go ahead and type the code in your file as shown.

On line 4, we create a variable called “numberChecked”. It has a type of “int” and an access modifier of “private”. We’ll go over what access modifiers are in a bit. An int is short for “Integer”, which is a whole number (no decimals).

Variables are like storage containers to store data we want to use later. All variables will follow this pattern of naming “access_modifier datatype name”, the “access_modifier” is optional. Here, the “numberChecked” variable will be used to keep track of how many words we have checked with this class. We start it equal to 0 because we start off with 0 words checked.

On line 6, we have another function. This function is public (again will touch on this later) and it’s name is “printStatus”. It also has a type, but because it is a function, this is called the return type. Some functions can give back data when they have completed, this one is of type void, void means nothing, so this function does not return any data. Even if a function does’t return data, we need to tell the program that it won’t by using void.

On line 7, we have another print. This will print out a message describing how many words the class has checked.

On line 8, we have the word return, this is what ends the function. Overall, we can see that the “printStatus()” function will be used to display how many words the “WordChecker” class has currently checked.