wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes const in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "const in java" Watch "const in java" New topic
Author

const in java

Nancy Antony
Ranch Hand

Joined: Sep 06, 2007
Posts: 145

Can anyone tell how to use const keyword in java. If it could be used, then what is the difference b/w const and final. If it const cannot be used then why there is a keyword available ?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

"const" is a reserved word, but it has no function in Java. You cannot use it.

The most likely reason why it's there is because it was inherited from C++. In the first version of Java, the language designers decided to reserve the word "const", with the idea of maybe implementing it in a later version of Java. But to date, it hasn't been implemented.
[ April 09, 2008: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Diego Fernandez
Greenhorn

Joined: Apr 10, 2008
Posts: 1
The same thing happens with keyword goto
Nancy Antony
Ranch Hand

Joined: Sep 06, 2007
Posts: 145

You are right.

I tried this code

class GotoDemo {

public static void main(String args[]){
System.out.println("prints hello 5 times");
int n=1;

printing5times:
System.out.println("Hello Nancy");
if ( n < 5) {
n++;
goto printing5times;
}
}
}

it gives compile time error.

Thanks for sharing knowledge.
 
 
subject: const in java
 
Similar Threads
Final
Fundamantals
Constant and Final
final and const
Questions on naming convention