<< Chapter < Page Chapter >> Page >

Answer and Explanation

Question 10

What output is produced by the program shown in Listing 10 ?

  • A. Compiler Error
  • B. Runtime Error
  • C. 2
  • D. 5
  • E. 10
  • F. None of the above
Listing 10 . Listing for Question 10.
public class Ap099{ public static void main(String args[]){new Worker().makeObj(); }//end main()}//end class definition class Worker{public void makeObj(){ NewClass obj = new NewClass(5);obj.x = 10; System.out.println(obj.x);}//end makeObj() }// end classclass NewClass{ private int x = 2;public NewClass(){}//end constructorpublic NewClass(int x){ this.x = x;}//end constructorpublic void setX(int x){ this.x = x;}//end setX() public int getX(){return x; }//end getX()}//end NewClass

Answer and Explanation

Listings

I recommend that you open another copy of this document in a separate browser window and use the following links to easilyfind and view the listings while you are reading about them.

Miscellaneous

This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: Ap0080: Self-assessment, Classes, Constructors, and Accessor Methods
  • File: Ap0080.htm
  • Originally published: 2002
  • Published at cnx.org: 12/05/12
  • Revised: 12/03/14
Disclaimers:

Financial : Although the Connexions site makes it possible for you to download aPDF file for this module at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, youshould be aware that some of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensationfrom the Connexions website even if you purchase the PDF version of the module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale onAmazon.com showing me as the author. I neither receive compensation for those sales nor do I know who does receive compensation. If youpurchase such a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it was made andpublished without my prior knowledge.

Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.

Answers

Answer 10

A. Compiler Error

Explanation 10

Variables declared private really are private

The code in the following fragment attempts to ignore the setter and getter methods and directly access the private instance variable named x in the object referred to by the reference variable named obj .

obj.x = 10; System.out.println(obj.x);

This produces a compiler error. The compiler error produced by JDK 1.3 is reproduced below.

Ap099.java:19: x has private access in NewClassobj.x = 10; ^Ap099.java:20: x has private access in NewClassSystem.out.println(obj.x); ^

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