• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Annonymous classes

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Instance initializers in annonymous classes can throw exception."

Can somebody make me understand with some example.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try compiling this:
 
Soni Prasad
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i guess i do both, else pls correct me
 
Maciek Makowski
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you, vishnu!

now that i read you, i should have known, because i knew about static initializer
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic