Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Java Question (Inb4 trolls)

Name: Anonymous 2012-03-20 11:01

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());
    }
}

Name: Anonymous 2012-03-20 11:03

You need a ; after X()

Name: Anonymous 2012-03-20 11:05

No, he doesn't.

Name: Anonymous 2012-03-20 11:22

op is a javafag

Code doesn't "work". It is executed or INTERPRETED.

Name: Anonymous 2012-03-20 11:55

>>4
Since it won't compile, it obviously doesn't execute.

Name: Anonymous 2012-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: Anonymous 2012-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: Anonymous 2012-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: Anonymous 2012-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: Anonymous 2012-03-20 12:57

Read: Anonymous Classes, function overloading

Name: Anonymous 2012-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.

Don't change these.
Name: Email:
Entire Thread Thread List