<< 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

summarize halerambos & holbon
David Reply
the Three stages of Auguste Comte
Clementina Reply
what are agents of socialization
Antonio Reply
sociology of education
Nuhu Reply
definition of sociology of education
Nuhu
what is culture
Abdulrahim Reply
shared beliefs, values, and practices
AI-Robot
What are the two type of scientific method
ogunniran Reply
I'm willing to join you
Aceng Reply
what are the scientific method of sociology
Man
what is socialization
ogunniran Reply
the process wherein people come to understand societal norms and expectations, to accept society's beliefs, and to be aware of societal values
AI-Robot
scientific method in doing research
ogunniran
defimition of sickness in afica
Anita
Cosmology
ogunniran
Hmmm
ogunniran
list and explain the terms that found in society
REMMY Reply
list and explain the terms that found in society
Mukhtar
what are the agents of socialization
Antonio
Family Peer group Institution
Abdulwajud
I mean the definition
Antonio
ways of perceived deviance indifferent society
Naomi Reply
reasons of joining groups
SAM
to bring development to the nation at large
Hyellafiya
entails of consultative and consensus building from others
Gadama
World first Sociologist?
Abu
What is evolutionary model
Muhammad Reply
Evolution models refer to mathematical and computational representations of the processes involved in biological evolution. These models aim to simulate and understand how species change over time through mechanisms such as natural selection, genetic drift, and mutation. Evolutionary models can be u
faruk
what are the modern trends in religious behaviours
Selekeye Reply
what are social norms
Daniel Reply
shared standards of acceptable behavior by the group or appropriate behavior in a particular institution or those behaviors that are acceptable in a society
Lucius
that is how i understood it
Lucius
examples of societal norms
Diamond
Discuss the characteristics of the research located within positivist and the interpretivist paradigm
Tariro Reply
what is Industrialisation
Selekeye Reply
industrialization
Angelo
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