File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Final keyword Query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Final keyword Query " Watch "Final keyword Query " New topic
Author

Final keyword Query

Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
Hello everybody,

Class A
{
final int x=2;
int k;
}
Class B
{
public static void main(String []args)
{
A obj1=new A();
A obj2=new A();

Question is will obj1 and ob2 will share the same memory for final variable"x". If not there is a wastage of memory.
}


Have the determination of mirror which never fails to reflect in spite of being broken into pieces.<br /> <br />Kiss the hands you cannot bite.<br /> <br />An Optimist is one who starts taking a bath when he accidentally falls into the water.
Owen Luo
Greenhorn

Joined: Mar 19, 2008
Posts: 5
I think obj1 and obj2 will have there own memory space for each variable 'x'.

And I think if you want to reduce the memory wastage by yourself, you should use keyword 'static final' instead of 'final'.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
Originally posted by Owen Luo:
I think obj1 and obj2 will have there own memory space for each variable 'x'.

And I think if you want to reduce the memory wastage by yourself, you should use keyword 'static final' instead of 'final'.
You are quite correct; if anybody wants to have one copy only of a field, then the field ought to be marked static.

And welcome to the Ranch.
Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
Thanks a lot both of you ....

And Welcome to the forum Owen !!!

Cheers,
Owen Luo
Greenhorn

Joined: Mar 19, 2008
Posts: 5
It's my pleasure to join Ranch.

I will try my best in JAVA.
 
 
subject: Final keyword Query