1

A question in "OCP Java SE 6 Programmer Practice Exams (Exam 310-065)" Assesment test 2.

Given:

public class WeatherTest {
static Weather w;

public static void main(String[] args) {
    System.out.print(w.RAINY.count + " " + w.Sunny.count + " ");
}

enum Weather {
    RAINY, Sunny;
    int count = 0;

    Weather() {
        System.out.print("c ");
        count++;
    }
}
}

What is the result?

A. c 1 c 1

B. c 1 c 2

C. c c 1 1

D. c c 1 2

E. c c 2 2

F. Compilation fails.

G. An exception is thrown at runtime.

The answer the book says C.

But when I try running this code I get compilation error, saying "The static field WeatherTest.Weather.RAINY should be accessed in a static way".

Which is correct and expected, but no one has complained about it on internet, so I am wondering if I am missing something? Has it got something to do with Java version or something?

Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
Syed Siraj Uddin
  • 583
  • 1
  • 5
  • 13

2 Answers2

2

The book is right and the correct answer is C, c c 1 1 as can be tested here.

This error is produced by your IDE which was promoted from a warning. Assuming that you copy-pasted all the code correctly. Don't use IDE when preparing for that exam, use simplest text editor and javac.

It has nothing to do with Java version, it works the same in all versions of Java SE 6 and higher.

Last but not least - never write code like that. This is only exam hokus-pokus...

Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
  • So i have tested this on JDK 1.6.18 and JDK 1.7.0.21 and it fails to compile, very odd... – Syed Siraj Uddin May 06 '15 at 16:57
  • @SyedSirajUddin Under Windows? Copy that text into file `WeatherTest.java`, compile with `javac WeatherTest.java` and run with `java WeatherTest`, what's the output? Are you using an IDE? Maybe it's because of it. I tested on my machine with 1.7.0_75 and `c c 1 1` is printed. – Adam Stelmaszczyk May 06 '15 at 17:05
  • yes @Adam you are right, I was using the IDE, I tried using the normal notepad thing and tried compiling with javac and running it with java, it works fine. I think it was the eclipse IDE which I was using was throwing a warning and I assumed that to be a compilation error. cool now I know the difference :) – Syed Siraj Uddin May 06 '15 at 17:18
  • Which version of eclipse are you using? Eclipse comes with internal java compiler. Be aware when you use eclipse old version. – Stephen Tun Aung Dec 17 '15 at 06:56
1

The code compiles and gives answer C.

All that is happening is that your IDE is issuing you with a warning that you should not access static members on an instance of a class, as it it confusing. w.RAINY makes it look like RAINY is an instance field, when in fact it is static. In this case w is actually null. The usual way to access static members is to use ClassName.member. Here you should write Weather.RAINY.

Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
  • I agree with this, but did not expected even a warning from a book of this stature but good to know whats going on behind the scenes. thanks – Syed Siraj Uddin May 06 '15 at 17:09
  • @SyedSirajUddin I think the book is probably really good, but this code is deliberately awful. It's designed to look like `w.RAINY` might throw a `NullPointerException` or the code might not compile or `count` might get incremented twice. You should not write code like that! – Paul Boddington May 06 '15 at 17:12