Using objects in Java

Description of the objects. How to create or instantiate them from the classes and their use.

The first thing to do to use an object is to have it, right? Well, we are going to first understand how objects are created and then move on to using them.

Object instantiation:

To instantiate an object, the method used is “new”. “New” creates an object of the class that we specify, but before this, the variable that will contain that new object must be declared. Let’s look at it in our Board class seen earlier.

Tokens stateBoard;

public Board(){ stateBoard=new Tiles ;};

These two lines include the entire mechanism for creating an object. In the first line, a variable “estadoTablero” of the class “array” is declared. The “array” class is a Java utility class that is included in “java.lang” and therefore, as I already commented once and will explain more later, it is available from any Java program.

In principle, this variable is empty, it does not contain any object, but it is “prepared” to contain an object of the “array” class.

In the second line of this code there is more crumb to comment: It is about the definition of a simple constructor for the “Tablero” class.

When a “new” statement is executed to create an object of a class, what it is actually doing is calling the corresponding constructor method for that class. For example if we execute this sentence:

Board myBoard = New Board();

We are calling the above method:

public Board(){ stateBoard=new Tiles ;};

See also  Dotted line with Photoshop CC

Therefore, what we achieve is to create the internal variable of “Board”, which we have called “estadoTablero” as an “array” object with dimensions 6×7, which are the dimensions of a “four in a row” board. As we can see, just like with our board class and any other class, we also use “new” to create the “array”.

Finally, and as you can see, to define a constructor for our class we simply define a method that as a peculiarity has the same name as the class in which it is defined. As for its access modifier, it is normally “public”, but it can also be “private”, in which case it will only be accessible from a “static” method of the same class. We will explain the latter better, for now it simply has to sound familiar to us.

Message passing:

As “message passing” is understood in object-oriented programming what we have always called in traditional programming: “call to a function”. Deep down if we start to think we basically see that:

  • First of all: Programs “on paper” are classes.
  • Second: Classes “on paper” are related to other classes. For example, our Tab class uses Java’s own “String” class to define its “color” parameter.
  • Third: In “real life”, ie in operation, classes are shown as “objects”.
  • As a conclusion: It is the objects that collaborate and relate to other objects, and they do it through “message passing”.

To execute a method of a class, use this syntax:

ClassName.MethodName(method parameters);

Message passing has an advantage over function calls in traditional languages. The advantage is that they always carry an implicit parameter: The object to which the method itself belongs. We see it with a very simple example.

See also  games in javascript

We imagine a method and a function that simplify a fraction passed as a parameter. The difference would be the following when using both the method and the function.

Traditional programming:

simple_fraction = simplify_fraction(fraction);

/*simp_fraction saves the result

to apply the function simplify_fraction

to the fraction parameter*/

Object-oriented programming:

FractionObject.simplifyFraction();

/* this case could be understood as

we simply tell the fraction to be

simplify to herself “as she knows” */

As we can see in Object Oriented Programming, the fraction to be simplified is not passed as a parameter since it is the same object that is the fraction and that in turn has the method to simplify it according to its internal attributes.

Access to internal variables of an object:

To access an internal variable of an object, it must be accessible to us according to the rules seen above. In case if we can simply access it with the following syntax:

ClassName.internalVariableName;

Loading Facebook Comments ...
Loading Disqus Comments ...