java objects tutorial for beginners

JAVA objects tutorial for beginners: JAVA objects introduction

As we said in the JAVA class Tutorial, a class is a blueprint used to create identical JAVA objects that respect the blueprint.

This means that the JAVA objects created have all the attributes, methods and inner classes defined in the class.

The object contains two main elements:

Attributes: represent the state of an object. Each object has its own values for each attribute of a class. Those values represent the state of an object. The state of an object may or may not change during the execution of a JAVA Program.

Methods: represent the behavior of an object (what an object can do). Those methods may be used to change the state of an object, to manage another object of another type, to do some computation and return a useful result, etc

All the objects created from a class have the same methods but their states are different.

JAVA objects tutorial for beginners: JAVA objects creation

JAVA objects are created using the new operator.

When the new operator is called, a special method called constructor is called.

We will deal with constructors in depth in a separate tutorial.

JAVA objects may be created in any JAVA method including the principal method: public static void main (String [] args)

Example:

The class definition is the following:

class MyFirstClass {
//attribute declaration
int attributeName1;
//methods declarations
//The default constructor
MyFirstClass(){
attributeName1 = 5;
}
void display(){
System.out.println(“attributeName1 = ”+ attributeName1);
}
}

We create a new object whose class is MyFirstClass using this single line of code:

MyFirstClass mfo = new MyFirstClass();

Here, we create an object of type MyFirstClass and we put its reference in the mfo variable.

mfo is called a reference type variable because it contains a reference to the newly created object.

A possible implementation of JAVA reference may be a pointer. But, JAVA itself doesn’t contain the concept of pointers.

The purpose of creating a class is to use it to create objects from that class. But, there is an exception (abstract classes) that we will deal with it later.

The MyFirstClass class contains a default constructor: MyFirstClass()

A constructor is a special method that:

  • Has the same name of the class
  • Doesn’t have a return type (not even void)

To know more about constructors, check the java constructor tutorial.

To test the creation of the object above, we should put for example the object creation line in the main method:

public class TestObjectCreation {
public static void main(String[] args) {
MyFirstClass mfo = new MyFirstClass();
}
}

JAVA objects tutorial for beginners: calling methods on JAVA objects and accessing attributes

We call a method on java objects using the dot notation.

mfo.display();

We access the values of the attributes of an object using the dot notation.

An example of a write access is the following:

mfo. attributeName1 = 8;

or we can display it using this line (read access):

System.out.println(“displaying the value of attributeName1: ”+ mfo.attributeName1);

We should put the MyFirstClass and TestObjectCreation classes in the same package (default package for example) for an access problem.

We will explain this when we deal with access modifiers and packages.

The final version of the TestObjectCreation class is the following:

public class TestObjectCreation {
 
public static void main(String[] args) {
MyFirstClass mfo = new MyFirstClass();
mfo.display();
System.out.println(“displaying the value of attributeName1: ”+ mfo.attributeName1);
}
}

The output got when running the TestObjectCreation class is the following:

attributeName1 = 5

displaying the value of attributeName1 : 5

This java objects tutorial for beginners arrives at its end. You can also check the oracle java objects tutorial.

Please don’t forget to subscribe to like our How to Program Facebook page or to subscribe to our newsletter to get our latest JAVA tutorials.

To check our core java tutorial, please check this link.

See you in the next core java tutorial.

Post a Comment

Previous Post Next Post