This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hi, I have two questions: 1. Is "null" a java keyword or a reserved word? 2. when we declare static members in a class, do we need to initialize them explicitly always....or can just leave the compiler to do that by default? if we can leave it for the compiler, can we say final in the statement? example: static final int v; does that mean that the compiler will initialize it to 0 and make it final? Thanks in advance... Neena
Hi, 1. null is not java keyword but this is reserved word. you can go and check more http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html 2. final modifer u can't modify value if u use final modifer in variable u can't override method if u use final modifer in a method u can't extends class if u use final modifer in a class 3. static 1. its initilize first in a class if u use static keyword 2. u dont need to create instance if use static keyword if u understand then ok otherwise can mail me take care aftab
poornima, null is not a keyword, but a reserved word, same as true and false. Check out the link aftababbasi gave, it defines them all on Sun's site. aftababbasi, we have a naming policy here at the Ranch, and your name doesn't comply. Could you please re-register with an appropriate name. You can get more info here: http://www.javaranch.com/name.jsp Thanks, Bill
Dear Bill I think true and false are boolean literal.
Joined: Jun 30, 2000
Nasir, you are correct, true and false are boolean literals and null is a null literal, but they are still reserved words and cannont be used. From the JLS: While true and false might appear to be keywords, they are technically Boolean literals (�3.10.3). Similarly, while null might appear to be a keyword, it is technically the null literal (�3.10.7).
Hi Neena, You can have a final variable (without the static modifier), in which case you can assign it a value at a later stage, but only once. And if you try to use it in an expression without having set its value, you'll get an error. You can not declare a static final variable without giving it a value. When you declare a variable static and final you are declaring a constant and must give it a value right away.
------------------ Hope this helps. Have a good day.
Hi Neena, if you declare a static variable in a class as static int i; then you don't need to give it a default value.Compiler would give it a default i.e. 0 in this case. if you declare a final static variable in a class as final static int i; then you must assign it a value in the same line where it is declared or in the static initializer block(Yes it works). This is same as if you declare a final non static variable in the class then you need to assign it a value in the same line or in every constructor. Hope this is clear. Regards, Hemant
Joined: Nov 05, 2000
That was a surprising bit of code posted by Serge Plourde. I always thought the compiler would not allow this.
I think it becomes clearer if you think about how you might use this functionality. For example, you might have a class Person - each person has a date of birth and that will be constant for each person but would be different for different people(instances of the Person class). You would want to set that date of birth in the constructor so that you could set a different date of birth for each instance of Person - you could give that final variable a value in the constructor. But once set in that constructor you couldn't change it afterwards. The compiler's quite strict about this - it makes sure that you set a value when you first declare that constant or that you set it in every constructor for class members. But if I have a class that deals with all sorts of spherical containers, I might want every instance of that class to have access to a constant Pi - so I could declare it as a static final float. But the code in the constructors would be executed every time I created an instance of the class and I only want that constant set once. So I have to set the value in code that is run at class load time - either setting the value as I declare the constant:- static final float = 3.14f; or in a static initializer which is run once at class load time. What I would like to know is:- Is there any good reason for creating this sort of class member:- final int constantValue = 4; Every instance of the class will be stuck with a copy of constantValue, irrevocably set to 4 - surely it might as well be declared static or it should be set in the constructor? Any ideas? Kathy
Joined: Nov 14, 2000
Sorry guys, The answer which i have given for first was wrong. i have got it cleared. But this is amazing i have read it from RHE certification guide. So, what you can say about this.