• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Oracle Certified Associate Java SE 8 Programmer 1 STUDY GUIDE EXAM 1Z0-808 Intr Assessment Test Q 12

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Oracle Certified Associate Java SE 8 Programmer 1 STUDY GUIDE EXAM 1Z0-808 Introduction Assessment Test Question 12 states:

1: public class Egret {
2:    private String color;
3:         public Egret() {
4:          this("white");
5: }
6: public Egret(String color) {
7:    color = color;
8:  }
9: public static void main(String[] args) {
10:   Egret e = new Egret();
11:   System.out.println("Color:" + e.color);
12: }
13:}

A. Color:
B. Color:null
C. Color:White
D. Compiler error on line 4.
E. Compiler error on line 10.
F. Compiler error on line 11.

The Study guide answer states:
B. Line 10 calls the constructor on lines 3–5. That constructor calls the other construc- tor. However, the constructor on lines 6–8 assigns the method parameter to itself, which leaves the color instance variable on line 2 set to its default value of null. For more information, see Chapter 4.

But the right answer seems to be F, because it is prohibited to call a private field outside the class. Am I right?
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yevhen Ikonnykov wrote:But the right answer seems to be F, because it is prohibited to call a private field outside the class. Am I right?

No, book answer is correct. The scope of class is the class body (the area between the braces i.e. { }).
Example:In your example main method is in between those two braces of Egret class and private fields are directly accessible within the class where they are declared so here private field color is accessed within the class but not outside of the class.
 
Ganesh Patekar
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please go through The Java™ Tutorials worth reading.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic