• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

what this code mean?

 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi all,
I got this code from my friend
this is the first time i am seeing this kind of code

and really i am confused to say what it really mean to say about.
First i thought that it might be some innre class(anonymous)
but it is not

can anybody help me out in this isssue
thanks in advance

regards
saikrishna
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it is an anonymous class.

Anonymous classes are automatically derived from class instance creation expressions by the compiler.

Anonymous Class Declarations
 
Ranch Hand
Posts: 44
1
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It can't be an anonamous class because InputStream is an actual java class. The API can be found at http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html

one of the rules of an anonamous class is that it cannot have an explicitly declared constructor. This class does.

If I'm not mistaken all this is a method of overriding the default read() method in the InputStream class on the fly instead of creating a class that extends the InputStream class. I have modified the class slightly to see its execution path and to execute the read method.

import java.io.*;
class test3{
public static void main(String args[])
{
System.out.println("hello 1");
final byte[] data ={1,2,3,4};
InputStream inputStream = new InputStream()
{
int index = 0;

public int read() throws IOException {
System.out.println("hello");
int val;
if(index >= data.length)
{
return -1;
}
else
{
System.out.println("hello 2");
val = data[index];
index++;
return val;
}
}
};

try
{
System.out.println(inputStream.read());
}
catch(IOException ex)
{
}
}
}

This would therefore be the rough equivalent of having your own class extending InputStream and over-riding the default read() method. e.g.
import java.io.*;
myInputStream extends InputStream {
int index = 0;

public int read() throws IOException {
System.out.println("hello");
int val;
if(index >= data.length)
{
val = -1;
}
else
{
System.out.println("hello 2");
val = data[index];
index++;
}
return val;
}
}
[ April 05, 2007: Message edited by: Quintin Stephenson ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quintin Stephenson claimed:


It can't be an anonamous class because InputStream is an actual java class.



This is wrong.
Clown fish live in sea anemones, not in anonamous (sorry, couldn't resist).

Don't know why Saikrishna thinks it is not an anonymous class.
It is.

Basic structure is like:

Watch out for the }; (not an emoticon).
And yes, ClassName can be also an actual java foundation class (if it isn't final).

You can read it like

You can make instances of this class (with no name, hence anonymous) on the fly, overriding a method (or 2,3...) and invoke the overridden method(s) on this instance.

Perhaps Saikrishna didn't see it is an anonymous class because there is also a different syntax.
In this different syntax you create an anonymous class inside the parentheses of a method call, eg


Everything in italics is inside the method call: Creation of an instance of an anonymous class that extends Flea.

In this syntax, you see the
}); non-emoticon instead of [b};[/b].


Yours,
Bu.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
umm err, I forgot,
Saikrishna's code doesn't have any output, perhaps try this instead:



But this can also be substituted by
System.out.println(1);


Bu.
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for you explanation Hassel :-) :roll:
 
Quintin Stephenson
Ranch Hand
Posts: 44
1
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hassel, you've convinced as well me that I was wrong with my previous statement in this thread.

Thanks for the correction.

cheers
Quintin
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

More about this anemones' classes, I forgot, they may also "extend" interfaces. This is frequently done with the Runnable interface to start a thread.

eg:



Bu.
 
Sunglasses. AKA Coolness prosthetic. This tiny ad doesn't need shades:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic