<< Chapter < Page Chapter >> Page >

Back to Question 10

Answer 9

C. 10

Explanation 9

A setter and a getter

This is a very simple program that uses a setter (modifier or mutator) method named setX to set the value 10 in a property named x that is stored in an instance variable named y in an object instantiated from the class named NewClass ..

The program also uses a getter (accessor) method named getX to get and display the value of the property named x . (Note that according to JavaBeans design patterns, the name of the property isunrelated to the name of variable in which the property value is stored.)

Back to Question 9

Answer 8

D. 5

Explanation 8

Hide your data and expose your methods

For reasons that I won't go into here, good object-oriented design principles state that in almost all cases where an instance variable is not declared to be final , it should be declared private . (A final variable behaves like a constant.)

What is private access?

When an instance variable is declared private , it is accessible only by methods of the class in which it is defined. Therefore, the only way that the"outside world" can gain access to a private instance variable is by going through an (usually public ) instance method of the object.

Accessor, modifier, mutator, setter, and getter methods

Historically, methods that have been defined for the purpose of exposing private instance variables to the outside world have been referred to as accessor and modifier methods. (Modifier methods are also sometimes called mutator methods.)

(Note that since the advent of Sun's JavaBeans Component design patterns, these methods have also come to be known as getter methods and setter methods indeference to the design-pattern naming conventions for the methods.)

A private instance variable with an initializer

The class named NewClass declares a private instance variable named x and initializes its value to 2, as shown in the following code fragment:

private int x = 2;

Two constructors

The class contains both a noarg constructor and a parameterized constructor as shown in the following fragment:

public NewClass(){ }//end constructorpublic NewClass(int x){this.x = x; }//end constructor

Calling the noarg constructor

If an object of the class is instantiated by calling the noarg constructor, the initial value of 2 remains intact, and that object contains an instancevariable with an initial value of 2.

Calling the parameterized constructor

If an object of the class is instantiated by calling the parameterized constructor, the initial value of 2 is overwritten by the value of the incomingparameter to the parameterized constructor. In this case, that value is 5, because the object is instantiated by the following code fragment that passesthe literal value 5 to the parameterized constructor. Thus, the initial value of the instance variable in that object is 5.

NewClass obj = new NewClass(5);

A getter method

Because the instance variable named x is private , it cannot be accessed directly for display by the code in the makeObj method of the Worker class. However, the NewClass class provides the following public getter or accessor method that can be used to get the value stored in the instance variable.

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask