wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonmyous class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anonmyous class" Watch "Anonmyous class" New topic
Author

Anonmyous class

sanjana narayanan
Ranch Hand

Joined: Nov 25, 2003
Posts: 142
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

Joined: Dec 18, 2003
Posts: 328
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
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
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

Joined: Jun 27, 2003
Posts: 75
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


XX.
sanjana narayanan
Ranch Hand

Joined: Nov 25, 2003
Posts: 142
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
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
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

Joined: Nov 25, 2003
Posts: 142
kathy,
Thank u for replying.
-sanjana
 
 
subject: Anonmyous class