kronberger kronberger - 1 month ago 18
Java Question

Needing help understanding piece of Java code

I started learn Java today, following by Head First Java, 2nd Edition, and I have some code, which is confusing to me. I've got experience in Python, but this is new. To be more specific, I don't understand what boolean canRecord = false;
actually do.

class DVDPlayer {

boolean canRecord = false;

void recordDVD() {
System.out.println("DVD recording");
void playDVD() {
System.out.println("DVD playing");

class DVDPlayerTestDrive {
public static void main(String [] args) {

DVDPlayer d = new DVDPlayer();
d.canRecord = true;

if (d.canRecord == true) {

Answer Source

On your class DVDPlayer you chose to say that a regular DVDPlayer cannot record. So you set it to false.

If you want it to record you either change the variable directly, like you did on the class DVDPlayerTestDrive.

The boolean canRecord = false is only meant to show you that it is possible to reproduce the behaviour of objects. In this case you have a class that should represent a DVDPlayer and as we know it the majority of DVDPlayers do not record.

Don't focus much on the code during the first approach to Object Oriented Programming (I assume it is your first OOP language too).

Try to understand the concepts and ideas behind it =)