Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Final keyword Query

 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
}
 
Owen Luo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50216
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot both of you ....

And Welcome to the forum Owen !!!

Cheers,
 
Owen Luo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's my pleasure to join Ranch.

I will try my best in JAVA.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic