<< Chapter < Page Chapter >> Page >

(The name of this method complies with JavaBeans design patterns. If you examine the name carefully, you will see why Java programmers often refer tomethods like this as getter methods.)

public int getX(){ return x;}//end getX()

Calling the getter method

Finally, the second statement in the following code fragment calls the getter method on the NewClass object to get and display the value of the instance variable named x .

NewClass obj = new NewClass(5); System.out.println(obj.getX());

Back to Question 8

Answer 7

C. Object containing 0, 0.0, false

Explanation 7

Default initialization values

The purpose of this question is to confirm that you understand the default initialization of instance variables in an object when you don't write code tocause the initialization of the instance variable to differ from the default.

By default, all instance variables in a new object are initialized with default values if you don't provide a constructor (or other mechanism) that causes them to be initialized differently from the default.

  • All instance variables of the numeric types are initialized to the value of zero for the type. This program illustrates default initialization tozero for int and double types.
  • Instance variables of type boolean are initialized to false.
  • Instance variables of type char are initialized to a 16-bit Unicode character for which all sixteen bits have been set to zero. Ididn't include initialization of the char type in the output of this program because the default char value is not printable.
  • Instance variables of reference types are initialized to null.

Back to Question 7

Answer 6

C. Object containing 5

Explanation 6

A parameterized constructor

This program illustrates the straightforward use of a parameterized constructor.

The class named NewClass defines a parameterized constructor that requires an incoming parameter of type int .

(For good design practice, the class also defines a noarg constructor, even though it isn't actually used in this program. This makes it available ifneeded later when someone extends the class.)

Both constructors are shown in the following code fragment.

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

The parameterized constructor stores its incoming parameter named x in an instance variable of the class, also named x .

(The use of the keyword this is required in this case to eliminate the ambiguity of having a local parameter with the same name as aninstance variable. This is very common Java programming style that you should recognize and understand.)

Call the parameterized constructor

The following code fragment calls the parameterized constructor, passing the literal int value of 5 as a parameter.

NewClass obj = new NewClass(5);

Hopefully you will have no difficulty understanding the remaining code in the program that causes the value stored in the instance variable named x to be displayed on the computer screen.

Back to Question 6

Answer 5

A. Compiler Error

Explanation 5

If you define any constructors, ...

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