<< Chapter < Page Chapter >> Page >

If you define any constructors, the default constructor will no longer be provided automatically. Therefore, if a constructor that takes no arguments willever be needed for your new class, and you define one or more parameterized constructors, you must define the noarg constructor when you define your class.

A parameterized constructor for Worker

The class named Worker in this program defines a constructor that receives two incoming parameters, one of type int and the other of type boolean . It uses those two incoming parameters to initialize two instance variables of the new object.

Oops!

However, it does not define a constructor with no arguments in the formal argument list (commonly called a noarg constructor) .

Calling the missing noarg constructor

The following code in the main method of the controlling class attempts to instantiate two objects of the Worker class. The first call of the constructor passes no parameters to the constructor. Thus, it requires a noarg constructor in order to instantiate the object.

public class Ap118{ public static void main(String args[]){new Worker().display(); new Worker(5,true).display();System.out.println(); }//end main()}//end class Ap118

A compiler error

Since there is no constructor defined in the Worker class with an empty formal argument list (and the default version is not provided) , the program produces the following compiler error.

Ap118.java:11: cannot resolve symbol symbol : constructor Worker() location: class Workernew Worker().display();

Back to Question 9

Answer 8

C. 0 0.0 false

Explanation 8

All instance variables are initialized to default values

All instance variables are automatically initialized to default values if the author of the class doesn't take explicit steps to cause them to initialized toother values.

The default values

Numeric variables are automatically initialized to zero, while boolean variables are automatically initialized to false. Instance variables of type char are initialized to a Unicode value with all 16 bits set to zero. Reference variables are initialized to null.

Back to Question 8

Answer 7

A. Compiler Error

Explanation 7

Pushing the compiler beyond its limits

Compared to many programming environments, the Java compiler is very forgiving. However, there is a limit to how far even the Java compiler iswilling to go to keep us out of trouble.

Initializing the value of a static variable

We can initialize the value of a static variable using an initialization expression as follows:

public static final MY_CONSTANT = initialization expression;

Important point

It is necessary for the compiler to be able to evaluate the initialization expression when it is encountered.

Illegal forward reference

This program attempts to use an initialization expression that makes use of the value of another static variable (myPI) that has not yet been established at that point in the compilation process. As a result, the program produces thefollowing compiler error under JDK 1.3.

Ap116.java:18: illegal forward reference = 2 * myPI;^

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