Anonymous Classes

Hey guys, is anyone able to explain quickly an example of why you’d need to use an anonymous class?
11 Replies
JavaBot
JavaBot7d ago
This post has been reserved for your question.
Hey @valer! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st7d ago
basically whenever you want a simple implementation for something that you only need in one place and don't want to put it in another file For example if you want to add a listener somewhere and that listener has two methods, you could do something along the lines of
mouseHandler.addMouseListener(new MouseListener(){
@Override
public void onMouseClick(MouseClickEvent event){
System.out.println("click");
}
@Override
public void onMouseRightClick(MouseClickEvent event){
System.out.println("clack");
}
});
mouseHandler.addMouseListener(new MouseListener(){
@Override
public void onMouseClick(MouseClickEvent event){
System.out.println("click");
}
@Override
public void onMouseRightClick(MouseClickEvent event){
System.out.println("clack");
}
});
You should always aim for your code to be readable and if it's more readable if it is in the same file, that's an option
JavaBot
JavaBot7d ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
valer
valerOP6d ago
Ok so its something that would only be in in the scope of one method correct?
valer
valerOP6d ago
Also, is this video a good representation of how anonymous classes work? https://www.youtube.com/watch?v=P_iOnmdww8E&t=81s
Bro Code
YouTube
Learn Java ANONYMOUS CLASSES in 5 minutes! 🕵️‍♂️
#java #javatutorial #javacourse public class Main { public static void main(String[] args) { // Anonymous class = A class that doesn't have a name. Cannot be reused. // Add custom behavior without having to create a new class. // Often used for...
dan1st
dan1st6d ago
if you only need it for assigning a single variable
dan1st
dan1st6d ago
I can't watch the video rn but you muäight want to check https://dev.java/learn/classes-objects/design-best-practices/
Dev.java: The Destination for Java Developers
When to Use Nested Classes, Local Classes, Anonymous Classes, and L...
When to Use Nested Classes, Local Classes, Anonymous Classes, and Lambda Expressions.
dan1st
dan1st6d ago
and https://dev.java/learn/classes-objects/nested-classes/ has a section about these as well
Dev.java: The Destination for Java Developers
Nested Classes - Dev.java
Defining a class within another class.
valer
valerOP6d ago
Hmm lets see
JavaBot
JavaBot6d ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
JavaBot
JavaBot5d ago
Post Closed
This post has been closed by <@1265852855458332673>.

Did you find this page helpful?