Will the following code work in Java? I have a feeling it should, but it refuses to compile, saying that X has no such method getText().
class X
{
//class body
}
class main
{
public static void main(String[] args)
{
X x = new X() { public String getText() { return "asdf"; } }; System.out.println(x.getText());
}
}
Code doesn't "work". It is executed or INTERPRETED.
Name:
Anonymous2012-03-20 11:55
>>4
Since it won't compile, it obviously doesn't execute.
Name:
Anonymous2012-03-20 12:02
For anyone that's interested, I figured it out. Turns out that getText() is a method previously defined in X; the declaration inside main() isn't adding a getText() method, it's just overriding the one that was already there.
Name:
Anonymous2012-03-20 12:02
//we should really put a trollguide somewhere
//newcomers will fall for anything
//herp herp
#include <iostream>
class a
{public:
/*class a*/int i/**/; a()/**?/{};
virtual ~a(){/*space for destructorz*/};
};
void main(){a *b = new a();
b->NonExistingMethod();
}
hey herp herp wut am i donnin wrongtcha?
Name:
Anonymous2012-03-20 12:08
Just put the getText method outside of the main method and in the X class. I don't know why would would put it in the main method in the first place. That's a stupid place to put a method.
Name:
Anonymous2012-03-20 12:19
>>8
Yes, it is. This isn't my code, I was trying to understand whether a new method was being added at runtime, or a previous method was just being overridden.
Name:
Anonymous2012-03-20 12:57
Read: Anonymous Classes, function overloading
Name:
Anonymous2012-03-20 13:00
>>10
I understand them both; code such as I posted is fairly non-standard, hence, I'd never seen it before and didn't know what it did.