• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonmyous class

 
sanjana narayanan
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I came across a statement when i went through a document on 'Java Tips'.
It says "Anonmyous class cannot have a const".
can someone explain abt this.
-Sanjana
 
Dmitry Melnik
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will have a default/inhereted constructor indeed. But since anonymous class has no name, and class name is used as constructors name, you can not technically define a constructor.
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy --
Hmmm... I wonder if they were using the word "const" to mean "constant", which in the Java sense means 'static and final'. Inner classes may not declare static members, UNLESS they are compile-time constants. So, an anonymous class must NOT define a static member, UNLESS that static member is also final, and has a value that can be resolved at compile-time.
To that means an anonymous class CAN have a constant.
For example, the following will compile:
import java.io.*;
public class Boof {
public static void main(String[] args) {
Serializable r = new Serializable() {
static final int x = 3; // THIS IS OK
}; // close inner class
} // close main
} // close class

But the following is NOT allowed:
import java.io.*;
public class Boof {
public static void main(String[] args) {
Serializable r = new Serializable() {
static int x = 3; // THIS IS NOTOK
}; // close inner class
} // close main
} // close class
And the following is also NOT allowed:

import java.io.*;
public class Boof {
public static void main(String[] args) {
Serializable r = new Serializable() {
static final int x = (int) Math.random(); // THIS IS NOT OK, even though it is final
}; // close inner class
} // close main
} // close class
cheers,
Kathy
"One test to rule them all. One test assigned them. One test to bring them all and in Prometric grind them."
 
O Joseph
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
howdy? how about this for moi part:
Hmm, anonymous classes - no name hence anonymous hmm?
constructors :- syntax should be something like this:-
<accessModifier><className>{
//constructor body
}
since tha anonymous class aint got no className how can it have a
constructor?
i hope this makes some sense.
cheers
 
sanjana narayanan
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think kathy is right. const is for constant and not for constructors.
But Kathy, can u tell me the reason why the following line give a cpmpile error.
static final int x = (int) Math.random(); // THIS IS NOT OK, even though it is final
-Sanjana
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy -- this is about the difference between:
* static final
* compile-time constant
A compile-time constant MUST be static and final, but it must ALSO be a value that can be resolved at runtime. So, the value of '3' can be locked down during compilation, but the result of Math.random() cannot be determined until runtime, so that's where the difference is.
So just because something is marked static and final does NOT necessarily make it a compile-time constant. Many times, this doesn't make any difference... but if the spec says "compile-time constant", then it definitely means that it must be static and final, AND that the value must be determined at compile-time, and not as the result of something that happens at runtime.
Does that make sense?
cheers,
Kathy
 
sanjana narayanan
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kathy,
Thank u for replying.
-sanjana
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic