• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Dan's ques - anonymous class

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what's going on here.. can someone please explain -
http://www.danchisholm.net/dec20/topic/section1/anonymous1.html


Question 3
abstract class A {
private int x = 4;
private int y = 2;
public int x() {return x;}
public void x(int x) {this.x = x;}
public int y() {return y;}
public void y(int y) {this.y = y;}
public abstract int math();
}
class B {
static A a1 = new A(2,1) {
public A(int i1, int i2) {x(i1);y(i2);};
public int math() {return x()+y();}
};
public static void main(String[] args) {
System.out.print(a1.math());
}}
What is the result of attempting to compile and run the program?
a. Prints: 8
b. Prints: 3122
c. Compile-time error
d. Run-time error
e. None of the above


and is


3 c Compile-time error An anonymous class declaration can not contain an explicit declaration of a constructor.

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since an anonymous class doesn't have a name, it can't have a constructor, because constructors have the same name as the class.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this out.
 
    Bookmark Topic Watch Topic
  • New Topic