Unusual inner class
11/03/2018 12:08
while doing a practice test for the SCJP exam , I came across aquestion with similar code to this:

import java.util.*;public class Question {
public static void main(String[] args){
Question q = new Question(); q.printInnerClassStuff(); }
public void printInnerClassStuff() {
// unusual inner class Base myBase = new Base(){ public int foo = 9; public int getFoo() { return foo;} };
System.out.println(myBase.getClass()); System.out.println(myBase.getFoo()); }}
class Base{ public int getFoo() { return 7;}}
It's a bit odd. I don'd know why such a construct would be allowed,nor what it might be useful for. Also I noticed that when the getFoo()method is commented out in the Base class, the compiler complains thatthe method getFoo() does not exist, yet '9' is printed out when it'snot commented out. I have no particular question, besides 'what theheck?'

Source is Usenet: comp.lang.java.help
Sign in to add a comment

Answer score: 5
11/03/2018 12:08 -
This is an anonymous subclass of a package-private toplevel class. If youdo not know about anonymous classes (and their various limitations andquirks) re-read your course materials ;)

Source is Usenet: comp.lang.java.help
Sign in to add a comment

eDiscover
Helpforce eDiscover provides technical articles updated each dayHelpforce eDiscover RSS feed contains the latest technical articles in RSS
Click the logo to go back to the main page
Search eDiscover
  
Categories

Click an icon to go to that category

Helpforce eDiscover contains articles about Microsoft Windows Helpforce eDiscover contains articles about Apple products and MacOS Helpforce eDiscover contains articles about Linux and POSIX operating systems Helpforce eDiscover contains articles about Helpforce Helpforce has a large variety of technical information and articles for you to read Helpforce eDiscover contains articles about databases, MYSQL, SQL Server Oracle Helpforce eDiscover contains articles about Java, JVM and the JRE Helpforce eDiscover contains articles about the QNX operating system Helpforce eDiscover contains articles about Oracle Solaris and Open Solaris Helpforce eDiscover contains articles about RISC OS, Acorn and the BBC Micro Helpforce eDiscover contains articles about Amiga and AmigaOS

Type your comment into the box below