How to create a Class and declare an Objects in Java

Now that you've read the last article about the usefulness of classes and objects in Java , this tutorial will teach you how to create a class in your IDE and how to instantiate objects of that class. 

For this, we use the concept of Object Orientation, creating a class called Student, which we will use in our next article to contain the name and student grades, as well as access the data and populate these objects.

How to create and use Class in Java


Let's start! First, we have to create a project.
I called mine FirstClass. 

By creating this, see that now we have a tab called FirstClass.java 
In this tutorial we will create another class, the class "Student", that Java will call "Student.java" 

For this, if you are using NetBeans, go to File -> New File 
Under Categories, select Java Class, and Next.
Give the name for its new Student class. 

Ready. Your new class is created. 
Note that appeared another tab next to the "FirstClass.java" and is called "Student.java" 

In this new file, you should see something like: 

public class Student { 

} 

It is our class, and which also is obviously blank. 
Created a class, we will learn how to create objects of that class. 


How to create/declare a object in Java


If you try to remember, you already have declared class objects in our Java course. 
More specifically, the object 'input' from 'Scanner' class, and several times. 

Now let's do the same with our class Student. 
Let's create an object, a student of class "Student". 
The syntax is (type this in the tab FirstClass.java): 

Student Geddy = new Student(); 
Student Alex= new Student(); 
Student Neil = new Student(); 

Okay, we create three objects of type Student. 

We could have done: 
Student Neil; 
Neil = new Student(); 

The new Student() is what creates the object. And assign it to variable Neil. 
This variable, however, is not an object. It contains a reference to the object. It's like it point to an adress in memory. So, whenever we change this variable, we are changing the object directly. 

However, it is very nerd say 'declare a reference to the Student object'. 
The day-to-day, we simply say: we create an object of type Student. 
But actually these variables are not objects, but references to objects. This is one important thing you must know, because you studied by Progressive Java course.

Methods or data that we put in class "Student" will be part of the objects "Geddy", "Alex" and "Neil". 
In the next tutorial you will see it in a deeper study. 

For example, if we want to add the field to store the full name them, we create a string within the class "Student", so all objects will have this string. 
What is the advantage of this? 

Well, we have created this field only once! Inside te class! And all objects inherit it! 
Imagine a real situation! In a school with thousands of students. You declare the string once, and it becomes part of the registration statement of thousands of students. 

Very useful that Java and Object Orientation, no? 

No comments: