aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Annonymous classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Annonymous classes" Watch "Annonymous classes" New topic
Author

Annonymous classes

Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
Posts: 97
"Instance initializers in annonymous classes can throw exception."

Can somebody make me understand with some example.


SCJP 1.4, SCBCD 1.3
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
try compiling this:


java amateur
Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
Posts: 97
On compiling your code error is

unreported exception java.net.MalformedURLException; must be caught or declared to be thrown
URL url = new URL(urlString);

but my question is according to this statement
"Instance initializers in annonymous classes can throw exception."

there should not be any errors on compiling and how can I declare that instance initializer will throw an exception for no compilation error??

soni
Maciek Makowski
Greenhorn

Joined: May 17, 2005
Posts: 11

but my question is according to this statement
"Instance initializers in annonymous classes can throw exception."

there should not be any errors on compiling and how can I declare that instance initializer will throw an exception for no compilation error??


The point is, for anonymous class you don't have to declare it:



This compiles fine. In case of normal classes, the only checked exceptions allowed to be thrown by instance initializers are these that have been delcared to be thrown by each constructor. Furthermore, the class has to have at least one explicit constructor. Anonymous classes don't have explicit constructors so the rule that applies here is that their initializers can throw any exception without the need for declaring it.

See JLS for details.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
from jls:
A given anonymous class is only instantiated at a single point in a program. It is therefore possible to directly propagate information about what exceptions might be raised by an anonymous class' instance initializer to the surrounding expression

so i refactored to this, which you can try:
Maciek Makowski
Greenhorn

Joined: May 17, 2005
Posts: 11
Originally posted by miguel lisboa:
[...] so i refactored to this, which you can try: [...]


How is your code related to the question and to the JLS quote you posted? You don't define any anonymous class and you don't have any instance initializers in your class.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i guess i do both, else pls correct me
Maciek Makowski
Greenhorn

Joined: May 17, 2005
Posts: 11
Originally posted by miguel lisboa:
i guess i do both, else pls correct me


1. An anonymous class is a class defined in an instance creation expression:



If you include the optional ClassBody in "new" expression, an anonymous class is created.

2. Instance initalizer is a top-level block of code in a class body. Your initial example had an instance initalizer, in the second one you just put a block of code in the main(String[]) method -- it is not top-level, hence it's not an instance initializer.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
looking closer at your former code, it gets totally hermetic to me:
Object a = new Object() {{FileInputStream fis = new FileInputStream("file.txt");}};

then, i had this idea that an annonymous class was one without name and inside another, period.

finally i was also wrong in thinking that Instance initializer was an initialization of some var...

so, btw, what's the name of what i coded? is it just an inner (annonymous) class?
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

so, btw, what's the name of what i coded? is it just an inner (annonymous) class?


It is not an inner class. You had instantiated an URL instance inside an instance initializer block in your first post in this thread.

About Instance initializer blocks:
Instance initializer blocks are executed every time when an instance of the class is created. They are used in anonymous classes to initialize fields as they don't have constructor.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
thank you, vishnu!

now that i read you, i should have known, because i knew about static initializer
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
import java.io.*;

class Checkep {
{
FileInputStream fis = new FileInputStream("file.txt");
System.out.println("instance block");
}

public static void main(String[] args) {
try {
Checkep c = new Checkep();
}catch(IOException ie) { }
System.out.println("main method");
}

Checkep() throws IOException {
System.out.println("constructor");
}

}

Since the FileInputStream is producing an Checked Exception the control returns and hence the print statement in the initializer block is not printing(Hope I am correct) . But why the constructor is not printing in the above class?
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
I got one more doubt regarding Initialization

From Khalid Mughal Book(page 332)

An initializer expression for a static field cannt refer to non-static members by their simple names.

int i =5;
// static int j =i; compiler error

Is there any other means of initializing an static field with non-static members. (OR) It is totally not possible to initialize an static field with non-static member.

I tried with a couple of examples and I think it is totally not possible to initialize an static field with non-static member.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
to get a better understanding of your code i re wrote it:

My conclusions:

1) if instance initializer executes in first place, then, because it throws an exception which inst declared or cought, constructor - so to speak- fails and so doesnt print anything

2) if i uncoment my code then everything prints

pls correct me if i'm wrong!

Thanks a lot, vishnu
[ May 26, 2005: Message edited by: miguel lisboa ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Annonymous classes
 
Similar Threads
Annonymous innerclasse
Anonymous Classes doubt
Annonymous Class constructors
interface implementation --- doubt
New methods in annoymous inner class