Anonymous Classes
Hey guys, is anyone able to explain quickly an example of why you’d need to use an anonymous class?
11 Replies
⌛
This post has been reserved for your question.
Hey @valer! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose 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.
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
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
💤
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.
Ok so its something that would only be in in the scope of one method correct?
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...
if you only need it for assigning a single variable
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.
Dev.java: The Destination for Java Developers
Nested Classes - Dev.java
Defining a class within another class.
Hmm lets see
💤
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 Closed
This post has been closed by <@1265852855458332673>.