• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

a final Q

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class newnew{
static int i;
static final int j;
final intk;
public newnew(){
i=1;
j=2;
k=3;
}
}
The compiler will complain "cannot assign a value to final variable j",but k is ok,why?
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
j is declared static, which means that it is associated with the newnew class, and not any particular instance of newnew. It must be initialized when the class is loaded, like this:
static {j = 2;}
or static final int j = 2;
k is not static and thus each instance of newnew has its own k, which can be initialized in the constructor.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. u can't use a static var in ur constructor;
2. here k is a "blank final", java permit u assign a value to it when runtime, once assigned it, can't change in this instance. so every instance can have a final var k, which is not equal. because u can init it at constructor.
3.static vars only one per class ,no matter how many instances were produced.
btw: are u a Chinese? me too
may be we can chat in Chinese.
 
Mike Lin
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But ,i is also a static ,why we can assign a value to i in the class constructor.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember that final static variables are commonly used to define CONSTANTS. Their purpose is that once defined they should not be change. For example, the constant Integer.MIN_VALUE is declared as final static.
Hope that makes sense.
 
John Paverd
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
j is final, which means it can only be assigned a value once, and never changed.
i is not final, so you can assign a new value to i as often as you want.
Imagine the following code being executed:
newnew n1 = new newnew();
newnew n2 = new newnew();
Java would assign a value to j twice, which is illegal, because j is final. Both n1 and n2 share the same j, because j belongs to the class, not the instances of the class.
 
Mike Lin
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
o,i know now .Thank you all very much. :-)
 
reply
    Bookmark Topic Watch Topic
  • New Topic