r/learnjava • • Nov 23 '23

What does <...> mean and what is this code doing?

    btn.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            System.out.println("Hello World!");
        }
    });

I'm trying to learn JavaFXa and I almost know nothing about Java. So btn is an instant of the class Button, an object. setOnAction is a method that's making it do something when btn is clicked, right? And the keyword new creates an object from the class EventHandler and passes it as an argument of the setOnAction method, but I have no idea what the rest of the code does. What does <ActionEven> do? Why are there brackets after the parenthesis? What does @Override do? It overrides the previous definition of a function and replaces it with the new definition?

0 Upvotes

14 comments sorted by

•

u/AutoModerator Nov 23 '23

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.

6

u/No_Policy9772 Nov 23 '23

Your question has already been answered but just a piece of advice.

If you truly don't have the time to learn all the basics of java but have an immediate need of JavaFx, I suggest using chatgpt whenever you come across any syntax you're not familiar with.

give it custom instructions like: "I am new to java and I'm currently trying to use JavaFx so whenever I post some java code asking for an explanation, please explain what that code is doing and also explain the java syntax."

this should help you understand java enough to understand what you're doing.

1

u/[deleted] Nov 23 '23

Thanks for the suggestion

10

u/Zeeboozaza Nov 23 '23

My suggested is to learn basic Java before jumping into learning JavaFX.

These are extremely basic questions, and you will save yourself a lot of frustration if you learn Java and object oriented programming principles within Java.

-15

u/[deleted] Nov 23 '23

I would like to, but I unfortunately don't have any time.

5

u/CanisLupus92 Nov 23 '23

It’s not going to be faster trying to do both at the same time.

1

u/[deleted] Nov 23 '23

I know what I'm trying to do is absolutely stupid and idiotic because I'm constantly hit with frustration of not understanding anything just as the guy above said, but the professor wants a gui program for the next week even though he hasn't taught us anything. I really don't know what to do and I don't think I can learn enough OOP before moving onto learning javafx and then writing the program in just a week.

1

u/Ieris19 Nov 23 '23

Lol, this sounds awfully similar to my college's first semester curriculum and general consensus. May I ask what country are you from?

Overall, knowing the basics in Java is going to help you tons, and if you ever need help, feel free to hmu, I can offer some guidance as a fellow student.

1

u/Zeeboozaza Nov 23 '23

You don’t need to read a whole java book. You can watch a 20 mini mute crash course on Java and Java OOP and you’ll at least be familiar enough to know what to google when you’re stuck.

2

u/Bunnymancer Nov 23 '23

You'll find that you'll have even less time after learning JavaFx...

2

u/nekokattt Nov 23 '23

This is a generic type variable, like List<Foo>. It specifies the specific type being used.

In this case, it is technically making an anonymous class you cant see extends the type after "new", and then passing an instance of that class to the method you are calling.

The override is saying "this method was defined in the superclass".

Key concepts to look up:

  • Overriding methods

  • Anonymous classes

  • Anonymous objects

  • Type variables / Generics

1

u/[deleted] Nov 23 '23

Thanks!