Constructors and Methods

As Day 7 is coming to a close I feel a little disappointed at how quickly time is passing us by. In class today and in groups we worked on constructors. A constructors is a block of code that initializes the newly created object. However, a constructor is not a method as it does not have a return type. * Whereas, a method is a collection of statements that are grouped together to preform an operation. When a program invokes a method the program control gets transferred to the called method. The called method than returns the outcome of one of the two following conditions – when the return statement is executed or it reaches the method closing bracket. ** Often in a method you may see this.name statement. What this does is differentiates between global and local variables within a scope. In this case by using the this.name it allows the method to access the contents of the object and execute the information inside.

Check out the example below to see a method in action.

Image taken from reference 2. See link below.
Image taken from reference 2. See link below.

The output of the method is displayed below.

Image taken from reference 2. See link below

As the method was able to access the contents of the object, the values associated with each variable was then in turn able to be displayed. Without the this.name you would not be able to access the contents stored locally or initiate them.

Attached below you will see an example of a group project that a classmate and I worked on using constructors. In this case we established all of our global variables before we proceeded to create a loop. In the loop we specified what arguments and parameters we want to use prior to creating our constructor.

Within the setup command we invoked our variable sun and set it to our array named i. We then created the name of our new constructor which we named Star. The new is the hint that we are creating a constructor. In the draw command we will create our background , stroke, and the stroke weight. Then we set the for loop for how we want the stars to be displayed. We then create our sun and rectangle. To make the sun and rectangle move we assign our x and y values to increase and subtract. This in turn allows our sun to move straight before disappearing when the edge of the canvas is met. Now you will see we invoke a method within our function star. In our function named Star we use the this.name. As we know in order to invoke a method a this.name is used. This is the dead giveaway. After the different methods are invoked we set the function to display. When all the code both local and global is invoked we get our image of a blue background, with shimmering stars, and moving shapes consisting of a circle or in this case our sun and a rectangle. The rectangle and circle almost remind me as if we were playing a psychedelic version of pong but missing a ball and player two. I hope this helped you to understand the difference between constructors and methods.



Used https://beginnersbook.com/2013/03/constructors-in-java/ as reference to constructors. *

Used https://beginnersbook.com/2013/03/constructors-in-java/ as reference to methods. **

Leave a comment

Design a site like this with WordPress.com
Get started