Java anonymous inner class 🎭

preview_player
Показать описание
Java anonymous inner class tutorial

#java #anonymous #class

public class Main {

public static void main(String[] args) {

/*anonymous class = an inner class without a name
only a single object is created from one
The object may have “extras” or "changes"
and no need to create a separate innerclass
when it only need it once.
Helps us to avoid cluttering code with a class name

Syntax is similar to a constructor,
except that there is also a class definition
within a block of code.
GREAT FOR LISTENERS
*/
MyFrame myFrame = new MyFrame();

}
}

public class MyFrame extends JFrame{

JButton button1 = new JButton("Button #1");
JButton button2 = new JButton("Button #2");
JButton button3 = new JButton("Button #3");


MyFrame(){


@Override
public void actionPerformed(ActionEvent e) {

}

});

@Override
public void actionPerformed(ActionEvent e) {

}

});

@Override
public void actionPerformed(ActionEvent e) {

}

});
}

}
Рекомендации по теме
Комментарии
Автор


public class Main {

public static void main(String[] args) {

/*anonymous class = an inner class without a name
a single object is created from one
object may have “extras” or "changes"
no need to create a separate innerclass
it is only need it once.
us to avoid cluttering code with a class name

is similar to a constructor,
that there is also a class definition
a block of code.
FOR LISTENERS
*/
MyFrame myFrame = new MyFrame();

}
}

import java.awt.event.*;
import javax.swing.*;

public class MyFrame extends JFrame{

JButton button1 = new JButton("Button #1");
JButton button2 = new JButton("Button #2");
JButton button3 = new JButton("Button #3");


MyFrame(){

button1.setBounds(100, 100, 100, 100);
button2.setBounds(200, 100, 100, 100);
button3.setBounds(300, 100, 100, 100);
button1.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
System.out.println("You pressed button #1");

}

});
button2.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
System.out.println("You pressed button #2");

}

});
button3.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
System.out.println("You pressed button #3");

}

});
this.add(button1);
this.add(button2);
this.add(button3);

this.setSize(500, 500);
this.setLayout(null);
this.setVisible(true);
}

}

BroCodez
Автор

Great content! Honestly one of the best coding channels out there. Keep up the great vids!

owenagnel
Автор

I love this guy's tutorials. I've learnt so much from him.

ryan_millard
Автор

Your examples are amazing. I Really gotta give you props on how clear and complete you make your explanations. From defining the actual word and giving it some context, providing a simple example, and then showing a use case. It's just top notch work my guy. I wish you the best!

frilledshrimpo
Автор

this is really high quality content!!, and your way in explaining the things make them so easy to understand; I was so scary from the inner class for months for now until this video.

ramien
Автор

Great content! Honestly, one of the best coding channels out there. Keep up the good work.

faizalimuhammadzoda
Автор

In Chinese there is something calls 匿名内部类,匿名对象,means a inner class or object without a name, it is convenient to use it if you want to use it only once a time.which always used to implement interface and override method, and it is very basic knowledge for us to know because if we don’t familiar with it, it will be hard for us to understand lambda expression.

girl
Автор

Gracias hermano <3
Something I really love of your videos is how you pronounce so clearly that even a non English speaker can understand

unlockme
Автор

I literally love you for the stuff you've done for us struggling

eugenezuev
Автор

Bro Code, you are very under-rated. Your tutorial videos are really helpful. Love from Myanmar.

lynnmyat
Автор

Excellent video. Thanks for the knowledge.

expansivegymnast
Автор

thank you bro <3 your way of explaining is very interesting and beautiful, love from algeria <3 .

bentameurmohammedayoub
Автор

I am learning so much from you bro, thank you

hyperkarlotheweeb
Автор

Great Video, you have a new suscriber now. Thanks

cheke
Автор

Hey thank you for the Java content Please make video about Arrays and maybe Array.list in while loops so I can print all after the loop its over

ShiroVK
Автор

Chinese teachers teaches their students by using very boring examples and it’s make me very hard to remember. And I really don’t know why they don’t teach me anything about Java swing classes such as JFrame or event. Action listeners. Your example is really easy to remember and it’s more interesting. And by the way, button1.addActionListener can be replaced by lambda expression so that it makes code more beautiful and easy to read.

girl
Автор

Something professional like "yo bro!"😂😂

vibingowsic
Автор

that´s neat bro, I had always done it by typing all that "crap" ( x'DDD) at the beggining, now you enlightened me haha, thanks sr !

rafaarteaga
Автор

I am really happy to see ur video as i expected and got what i need ..tqqq so much...

kiruthikas
Автор

Don't know what Jframe is but thank you so much for explaining<3

Dankake