About Question enthuware.ocajp.i.v7.2.917 :
Posted: Mon Oct 08, 2012 3:17 pm
Given:
public class Square {
private double side = 0; // LINE 2
public static void main(String[] args) { // LINE 4
Square sq = new Square(); // LINE 5
side = 10; // LINE 6
}
}
What can be done to make this code compile and run?
Correct answer (according to the software Enthuware) is:
replace // LINE 6 with: sq.side = 10;
Because: side is not a global variable that you can access directly (Note that Java doesn't have the concept of a global variable). side is a field in Square class. So you need to specify which Square object's side you are trying to access.
My question is: given that side is already defined as private, it wouldn't be accesible even if I specified the object it belongs to, right? Or what am I missing here?
public class Square {
private double side = 0; // LINE 2
public static void main(String[] args) { // LINE 4
Square sq = new Square(); // LINE 5
side = 10; // LINE 6
}
}
What can be done to make this code compile and run?
Correct answer (according to the software Enthuware) is:
replace // LINE 6 with: sq.side = 10;
Because: side is not a global variable that you can access directly (Note that Java doesn't have the concept of a global variable). side is a field in Square class. So you need to specify which Square object's side you are trying to access.
My question is: given that side is already defined as private, it wouldn't be accesible even if I specified the object it belongs to, right? Or what am I missing here?