Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compile Time Constant

 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Why I am getting an error at Line #7 . If I replace Line 3 and Line 4 with final int b = 2; I don't get error. This is called Compile Time constant..what is the logic behind this.
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read this compile time constants tutorial...
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ankit, I raised this question based on your written tutorial. How the compiler treats the following 2 ways of declaring a variable. I fee both are exactly same but compiler thinks they are different..What's happening..

and
 
Simone Aiello
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
....
 
Simone Aiello
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

is like write


the compiler doesn't ever know b will be initialized to 4, even if it's obvious that it occurs...
 
Henry Wong
author
Marshal
Pie
Posts: 21357
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic