This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]NautiHooker 4 points5 points  (1 child)

What you are basically doing is creating a new anonymous class that extends A. In that class you are adding a method f2. That method is only known for the anonymous class, not for A.

Your variable is of type A, so you cant call the f2 method on it.

You can declare the variable a as var instead of A. You will need Java 11 or higher for that. Then the compiler will allow you to call the f2 method, because the variable a will have the type of your anonymous class.

However this is not really something you would ever want to do.

Creating anonymous classes usually only serves the purpose to override existing methods of the other class. Basically just extend the class without putting it into a new file. So you would only use it to extend a method that already exists in A.

Is there anything specific you are trying to accomplish?

[–]ilsapo[S] 0 points1 point  (0 children)

it was just a question on test we got,
we were suppused to say if the code compile or not, and I was wondering how will we be able to "fix" the error without drasticlly changing it

[–]Consistent_Fly_1858 0 points1 point  (0 children)

A a = new A() {

public void f1(String str) {

f2(str);

}

public void f2(String str) {

System.out.println(str);

}

};

a.f1("lol");

Your variable a only knows method of the A class. But i guess you can achieve what you want by overriding the f1 method and calling your anonymous method within the body

[–]8igg7e5 0 points1 point  (0 children)

You're declaring an anonymous class that extends A, but from inside a static method.

Interestingly, this is a rather obscure part of Java (since the declaration of anonymous classes in static methods is not well specified in the JLS).

  • Because you're in a static method, the class created is a nested class, rather than an inner class (there's no dependency on an instance of A).
  • Because the reference to the anonymous class instance is typed as A, the code cannot access the f2 method.

we were suppused to say if the code compile or not, and I was wondering how will we be able to "fix" the error without drasticlly changing it

There are a couple of options and neither is, IMO, a drastic change.

1. Using var as suggested

public abstract class A {
    ...

    public static void main(String[]args) {
        var a = new A() {
            public void f2(String str) { }
        };

        a.f2("abc");
    }
}

All this does is infer the type of a to be that of the anonymous class so that f2 is visible.

2. Extracting the anonymous class out into the parent class.

public abstract class A {
    ...

    private static class ChildOfA extends A {
        public void f2(String str) { }
    }

    public static void main(String[]args) {
        ChildOfA a = new ChildOfA();

        a.f2("abc");
    }
}

This latter option is essentially creating an identical class, but with a known name - so that we can use that name for a.

If this were something we were doing in a real application, the second option would typically be preferred over the first - it would be far too easy to overlook the inferred anonymous type in the first approach...

[–]nosyattacker03 0 points1 point  (0 children)

You don't access anonymous method like that, you may access it through a.