aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Compile Time Constant Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Compile Time Constant" Watch "Compile Time Constant" New topic
Author

Compile Time Constant

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
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.


OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Read this compile time constants tutorial...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
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

Joined: Oct 13, 2010
Posts: 22
....


The important is not what you know, but when you know it...
Simone Aiello
Greenhorn

Joined: Oct 13, 2010
Posts: 22

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
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

I wrote an explanation a while back...

http://www.coderanch.com/t/454384/java/java/compile-time-constant

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compile Time Constant