Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

questions on static members and null

 
Neena Shah
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sahir Shah
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
Rgds
Sahir
 
asim wagan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
1)null is a java keyword.
2)For static final variables you have initialise them, else compiler will give errors.
 
aftababbasi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 viswa
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Bill
I think true and false are boolean literal.
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4369
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hemant Bhaiya
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sahir Shah
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That was a surprising bit of code posted by Serge Plourde. I always thought the compiler would not allow this.
 
Kathy Rogers
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
asim wagan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
cheers
Oliver
 
Aniruddha Mukhopadhyay
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Aniruddha
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic