• 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

Questions with many areas

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to know about whether there will be a question as follows, in the SCJP exam. This question covers many areas. By changing little by little I got this question.

class A{
public static A A(){
return new A(){};
}
public static void main (String args[]){
System.out.println(new A().toString() + new A$1());
}
}
 
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure this compiles what is A$1?.Where is annonymous inner class?


If its a SCJP question answer is compilation fails..


Shrinivas
 
Lanka Prasad
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it worked.
the anonymous class is there with {} -> return new A(){};
You may see the compiler makes A$1.class file in the path.
 
Shrinivas Mujumdar
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rather you can't instantiate annonymous inner class in this fashion
i.e.


part of your code new A$1()



So compilation fails......it will be instantiated when you give a call to static method



Output

A@16930e2 A$1@108786b

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

Yes, it worked.
the anonymous class is there with {} -> return new A(){};
You may see the compiler makes A$1.class file in the path.



You are Correct Lanka Prasad...Kindly refer earlier post
 
Lanka Prasad
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shrinivas Mujumdar:


You are Correct Lanka Prasad...Kindly refer earlier post



Yes, thanks for the reply, but I hope Sun wont ask this kind of questions.

If you remove {} in the static method. println line gives compilation error.
(You have to delete A$1.class befre compile otheriwse still no CE.)
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lanka Prasad:
...I hope Sun wont ask this kind of questions...


You should definitely be prepared to recognize this. If "new ______()" is followed by braces before the semicolon, then you have an anonymous inner class.
[ February 20, 2006: Message edited by: marc weber ]
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Yes, it worked.
the anonymous class is there with {} -> return new A(){};
You may see the compiler makes A$1.class file in the path.


But when i am trying to run the given code I am getting the compilation error.

java.lang.NoClassDefFoundError: A$1
Exception in thread "main"

I hope it can be compiled and executed only when we repalce new A$1() with A.A() in given code.

Plz confirm....

[ February 20, 2006: Message edited by: gaurav singhal ]

[ February 20, 2006: Message edited by: gaurav singhal ]
[ February 20, 2006: Message edited by: gaurav singhal ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because an anonymous class has no name, it cannot be instantiated anywhere other than at its declaration.
 
reply
    Bookmark Topic Watch Topic
  • New Topic