• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding compile time constants

 
vikram dev
Greenhorn
Posts: 3
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following code snippet,

final int s = 34 ;
final int d ;
d = 43 ;

Why would the variable d not be considered a compile time constant by the compiler?

And can somebody tell what kind of questions can be expected in the OCAJP 7 exam?
I mean, will all of the questions involve solving code snippets or any theory based questions like the questions in ENTHUWARE mock tests?
Thank you
 
Sergej Smoljanov
Ranch Hand
Posts: 468
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in actual exam you can expect theory question and question that you read code and ask for output.
for this one you can expect question about switch. or for example like this one
what difference in output?

enthuware question is very good examples of type question than you can meet at the exam.

Why would the variable d not be considered a compile time constant by the compiler?

because you split declaration and assignment.
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergej Smoljanov wrote:
Why would the variable d not be considered a compile time constant by the compiler?

because you split declaration and assignment.

This is described in detailed in section 4.12.4 of the Java Language Specification ...

https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.12.4

Variables declared as final and without an initializer is considered a blank final variable. The following line of that section of the JLS talks about constant variables -- and initialization is a requirement of declaring such a variable (compile time constant).

Henry
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vikram dev wrote:And can somebody tell what kind of questions can be expected in the OCAJP 7 exam?
I mean, will all of the questions involve solving code snippets or any theory based questions like the questions in ENTHUWARE mock tests?

The actual exam contains both questions with code snippets and theory-based questions. You will have more questions from the first type than the latter type. And as Sergej already mentioned: Enthuware questions are an excellent representation of the questions you'll encounter on the actual exam.

Now let's see if you fully understand the theory about compile-time constants. Which of the following variables are compile-time constants?

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic