Classes and Objects - JAVA: Easy Java Programming for Beginners, Your Step-By-Step Guide to Learning Java Programming (2015)

JAVA: Easy Java Programming for Beginners, Your Step-By-Step Guide to Learning Java Programming (2015)

Chapter 9. Classes and Objects

You have already been encountering Java classes and objects in the previous chapters of this eBook. At this point in time, you will gain more knowledge on the available class variables and objects that you can employ to achieve the desired results of why you are programming in a Java environment.

Classes

Acting as blueprints of a Java program, classes are templates that describe the characteristics of an element or method. Basically, they are generic elements in Java programming. They also help a programmer to understand the coding system of another programmer, making its structure clear. Classes exist in the program as long as they desire, meaning they do not have a lifespan.

The following is a list of possible class variables:

· Class - A class is a variable that needs to be declared before it enters a class, which can be found within any class and outside any method.

· Instance - Instance is a variable that remains within a particular class but is outside a method. However, it is accessible from any other method once declared.

· Local - A local is a variable that is defined inside any method in the program and requires initial declaration and acknowledgement. It is omitted after it has executed the command or function.

Objects

Java objects are elements that possess behaviors and states. When elements are defined, they come with their own features that further adds value to a program component without having to include an extensive feature. These are what you call instances of classes. Because of the behaviors and states of objects, methods are executed successfully. Furthermore, a particular object is associated with a unique function or command so it will adhere to certain instructions. In contrast with a class, an object ceases to exist once the program has been executed.

One of the characteristics of object-oriented programming is organizing things and concepts. There are three object relationships that defines why elements should or should not be moved to another particular program component:

· Is-a Relationship – this means that a type of object is more specific than its fellows (number 1 is a number)

· Has-a relationship – this means that a type of object contains or is associated with another object (given number 1 and number 2: number 1 has a succeeding number, number 2)

· Uses-a relationship – this means that a type of object will be using another object as a program progresses (given number 1, number 2 and number 3: number 1 uses a number 2 to arrive at the sum of number 3)

It is important that you know how to work around with classes and objects when programming using the Java language since they are considered as the generic elements of the software. You have also learned how they are organized and their existing relationships with one another. For the last but not the least chapter, you will be given an idea on what constructors are all about.