File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes questions on static members and null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "questions on static members and null " Watch "questions on static members and null " New topic

questions on static members and null

Neena Shah

Joined: Nov 18, 2000
Posts: 9
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?
static final int v;
does that mean that the compiler will initialize it to 0 and make it final?
Thanks in advance...
Sahir Shah
Ranch Hand

Joined: Nov 05, 2000
Posts: 158

A final variable is effectively a constant. You must always assign a value to a constant. Not doing so defeats the whole purpose of defining a constant.

asim wagan
Ranch Hand

Joined: Nov 14, 2000
Posts: 62
1)null is a java keyword.
2)For static final variables you have initialise them, else compiler will give errors.

Joined: Nov 22, 2000
Posts: 4
1. null is not java keyword but this is reserved word.
you can go and check more
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

Keep Smilling
poornima viswa

Joined: Nov 18, 2000
Posts: 16
I have a doubth.
i think null is not a java keyword nor is a reserved word.
can anyone clarify this
thanks in advance.
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
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:
Thanks, Bill
Nasir Khan
Ranch Hand

Joined: Nov 04, 2000
Posts: 135
Dear Bill
I think true and false are boolean literal.
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
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).
Serge Plourde
Ranch Hand

Joined: Jun 23, 2000
Posts: 140
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.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4351

as far as a static but not final I believe the compiler will give it a default value as long as its not a local variable.

Visit my download page
Hemant Bhaiya

Joined: Nov 01, 2000
Posts: 21
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.
Sahir Shah
Ranch Hand

Joined: Nov 05, 2000
Posts: 158

That was a surprising bit of code posted by Serge Plourde. I always thought the compiler would not allow this.
Kathy Rogers
Ranch Hand

Joined: Aug 04, 2000
Posts: 104
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?
asim wagan
Ranch Hand

Joined: Nov 14, 2000
Posts: 62
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.
Oliver Grass
Ranch Hand

Joined: Nov 02, 2000
Posts: 65
Hi Asmin,
hmmm, you have to read this RHE carefully
They are saying...
The Java language specifies 50 keywords and other reserved words which are listed in Table 1.1

The heading of this table only mentions java keywords, BUT there are keywords and reserved words....
Aniruddha Mukhopadhyay
Ranch Hand

Joined: Nov 15, 2000
Posts: 59
static final variable need NOT be given value on the same it is declared - it may be done later in static initializer block as well.
As is a class variable, it is not possible do it in constructor.


I agree. Here's the link:
subject: questions on static members and null
It's not a secret anymore!