Inner Class Java Tutorial - Creating and Using Inner Classes
HTML-код
- Опубликовано: 20 июн 2021
- Complete Java course: codingwithjohn.thinkific.com/...
Inner Classes can be a confusing topic when you're a Java beginner (or maybe even a bit more advanced...)
But creating an inner class in Java is way easier than it seems, and whether you're a Java beginner or further along in your Java career, this video will have you creating your own inner class in Java in no time!
We'll talk about 3 different types of Java Inner Classes, how you can create them, and how you can use them.
Learn or improve your Java by watching it being coded live!
Hi, I'm John! I'm a Lead Java Software Engineer and I've been in the programming industry for more than a decade. I love sharing what I've learned over the years in a way that's understandable for all levels of Java learners.
Let me know what else you'd like to see!
Links to any stuff in this description are affiliate links, so if you buy a product through those links I may earn a small commission.
📕 THE best book to learn Java, Effective Java by Joshua Bloch
amzn.to/36AfdUu
📕 One of my favorite programming books, Clean Code by Robert Martin
amzn.to/3GTPVhf
🎧 Or get the audio version of Clean Code for FREE here with an Audible free trial
www.audibletrial.com/johnclean...
🖥️Standing desk brand I use for recording (get a code for $30 off through this link!)
bit.ly/3QPNGko
📹Phone I use for recording:
amzn.to/3HepYJu
🎙️Microphone I use (classy, I know):
amzn.to/3AYGdbz
Donate with PayPal (Thank you so much!)
www.paypal.com/donate/?hosted...
☕Complete Java course:
codingwithjohn.thinkific.com/...
codingwithjohn.com
Watching this again I just realized I made a mistake with the editing in part of the intro, where I had my face in the corner and a blank rest of the screen. Oh well, hope you all can forgive such a grievous mistake >_>
I never noticed!
Have been programming in java for years but somehow im hooked on all ur videos lol. Think its learning how to explain concepts in an easy way, ur so darn good at that. Also, had no idea you could create classes in a method! I keep learning
Same here dude! 🙌
Cool bro
This mans gonna get me through university
Poland Computer Science student says thank you !!
The way you teach is absolutely awesome!
>Has never seen this being used
>Still makes a video about it
Big hustler energy!
You just made me interstand this concept after days of frustration. Thanks a lot!
Spent the _whole_ day yesterday reading a book by C. Horstmann, trying to understand the syntax of outer/inner class, trying to understand how the _"this"_ keyword figures into this whole thing, and by the end of the day, I was more confused than I was at the start. I watched this video today and just 8 minutes into it, I understood nearly everything.
I've written a lot of different inner classes, but I still learned some things from the video. Kudos to you John!
I learn something new with you every time I revisit familiar subjects. You are great!
Dude, you’re amazing at explaining Java. Thank you so much for these vids!
John your style of teaching is simply fabulous. you can make learn a person easily. thanks John.
The best tutorial. I'm so glad to stumble upon this video. Concise. discusses the key important points of inner classes and examples. Very easy to understand how you speak. Well done.
This Video just saved me in my Exam, thank you for the great Video
I like how simple you make things and explain them , very useful video 👍👍
Hi John! I'm really thankful to you for all of your videos you really explain very well you make Java looks easy :) so I wanna ask you a video explaining design patterns of course when you can do it.
Appreciate this explanation about static inner classes, for some reason I keep forgetting what makes it static. Great vid!
Thank You very much john, ur teaching style is soooooo awesome I got the info which I was searching for . Hatss offff....
👍👍🙏
I've had already read the documentation and understood how ir works but decided to see a video about it, because I was fatigue. And have to admit, your content is amazing. You explained this very fast and well. Congrats! Just sub to the channel. It'll help a lot for sure. Tks for the video.
Thanks john, you are a life saver!
This tutorial is great John!!!
Thanks a lot for making this video, helped me a lot.
I was waiting until I found a case of inner class before I watched this video. Since you do a great job of instantiation. I found it looking at some example code for the ITERATOR DESIGN PATTERN.
You explain things by telling whats the problem or need of the topic that you're going to explain by giving a example case, It helps so much when i know where can be the concept that i am learning be used instead just watching plain theory tutorials which don't tell the cases where it can be used or whats the problem that may arise if we do things other way.
Thank you soo much, u made this so easy omgggg!!
I think i just found the way to solove my problem i have with my code after 10 hs of searching, thank you!
Great lesson, John! Thanks!
Best thing I have learned from this channel is to never be lazy when typing names of stuff 🤣
So innerclass are just normal classes in different locations, which you can access with Outername.Innername or inside the method when you create an object there. Got it. Thank you, this really blew my mind.
exactly 2 years later I'm watching this, watched last year for the first time actually :)
We use theese to program our frc robot, we have a constants class then inside od the constants class there are inner classes for each subsystem. It its really helpful when you want to keep everything divided and prevent yourself from mixing things up.
This is great, and is also a great pre-req for an Anonymous Inner Class video you haven't made yet. Between lambdas and default methods in interfaces having been added to Java these days, you don't need anonymous inner classes anywhere near as often as you used to, but sometimes you do, and in older code they were used all over the places, not having those new facilities available yet before Java 8, so if you work with older code sometimes....
While there are a lot of weird little rules to remember about different kinds of inner classes in Java, that can sometimes get a little tricky, this is a fantastic introduction to the topic. Last detail, some people insist on always referring to the static kind as "nested" and the member kind as "inner", to try to keep things clear. I am often going to those sorts of people for help, so I try to stick to that.
That's great info, thanks!
Amazing thanks, you videos are the best!
That was just GREAT!
You didn`t waste my time!
this was so clear! thanks Johnny, from Noga and Elai
Leaving a comment for the RUclips algorithm because you’re super helpful 😁
Could you do a video about packages in Java? Btw, good explanation, I was struggling to understand inner classes but now is all clear. Thank you
I've recently learned how to use an anonymous inner class to instantiate something like a HashMap and put values straight away. It would be good to learn more about anonymous inner classes and their uses.
Thanks so much ❤️, now I got what was Map.Entry
Just wow! New subscriber 💯.
Thank you so much ❤
Node in LinkedList and Pair in Map are great examples for local inner classes
Hey you did it! Thank you so much!
Hope it helps, thanks for watching!
@@CodingWithJohn it sure does, keep up the good work!
This is always a pleasure to Watch your vidéos courses, thank you very much John for all your java code passion !
Hi John!
Love you videos!
Would you maybe consider making a few videos about Spring? :)
Kind regards!
Nice tutorial
This guy about to save my semeseter
You are born to teach. Can you please cover design patterns specially SOLID if possible?
You should do a java data structures series
short and straight to the point, I alwyz run away from nested classes. I find them clamssy & difficulty, lol, but from the video, am hooked and eager to use them. Not that difficult... lol
thank u very much !
Hello John!!! I am truly grateful the great work your doing.. Please help me with concerning serialization, deserialization and externalisation. Thank you
Thank you
3:37 Oh wow, you learn something new every day XD
Thanks 👍👍
¡Gracias!
Thanks, I am a big fan of you!! Can you give some real time examples for better understanding..
Nested classes are very useful to avoid namespace pollution. Being able to define a static class called "Loader" is a lot less confusing than having a separate class for each thing that needs to be loaded. Instead of your IDE trying to suggest a bunch of loaders all the time.
Thanks for tutorial! Simple and clear! Do you have a video explaining interfaces?? How can I use them in game development??
Thanks for watching! I don't have a specific one yet, though the video on abstract classes touches on interfaces a bit.
Basically an interface can be thought of as a contract; if you have a class that "implements" an interface, that is a guarantee that the class will have certain methods that the interface specifies you have to implement.
Hard to give a specific example for game development, since it's very broad and depends on what you're trying to do in your code. But as a general example, maybe you have many different classes in your game that represent things that can make noise. Character, NPC, Enemy, Animal, Item, etc. They might not be related through normal Java inheritance. You can perhaps have some interface like MakesNoise, that specifies that each class that implements the interface has to implement a makeNoise() method. Then each of those classes can implement that interface, then just implement their own version of makeNoise(). Then anywhere in the code you know that anything that implements that interface is guaranteed to have that method available.
@@CodingWithJohn Thank you a lot for replying me. I am developing kind of space shooter like "Space Invaders" with JavaFX for the Univercity course. Using one interface as a storage for constants like GAME_WIDTH. Now I want to use interfaces bit wider. Your explanation is clear to me. Thanks one more time.
One thing missing in this video, explaining why would anyone use inner classes.
John, can you give a real life example where an inner class would only make sense to be inside a outer class to help us understand the scenarios?
They work well for action listeners...
Builders
I want an example of when it’s more useful to do the inner class than a extended class
Broadly, I mostly use them when the inner class I'm making should never be used in any other context than that specific outer class, and you don't want anyone using it by mistake.
There's one circumstance where I use them often at work - it's a bit of an advanced situation for Java beginners so I didn't talk through it in the video, but I can describe it here.
The applications we build at my job are web services, and we also often make calls out to other web services to gather more information we need. The structure of the response we receive from those other services is designed by that other team, and we don't have any control over it. But we need to take their response body and map it to a Java object so we can work with it, so we need to represent that response type on our application with a class we make.
Their response body might look something like this:
{
errors: [
{
clientErrorCode: "12345",
clientErrorDescription: "You sent something bad"
}
]
}
The "errors" inside that errors list should be their own class, and have a clientErrorCode and clientErrorDescription field, probably with getters and setters. But that class only makes sense in the context of this client's response. Sure, it's possible that right now it might fit in with some other class that has a code and description, but I wouldn't want to couple it with any other functionality in my code that doesn't relate to processing a response from this client. So if this client changes the structure of the response I just have to change this class, and no other code is affected. If I didn't make it an inner class, some other developer might find the class I make called "Error" and use it for something else that it shouldn't be used for, since it only makes sense here.
@@CodingWithJohn thank you. That makes a lot of sense
Thank you, i am learning a lot from you John
@@CodingWithJohn Perfect example! I was struggling to grasp some real use cases for inner classes, thank you very much
CS student here, we recently learned about linked lists and I'm assuming one of the best ways to implement a node class for a linked list is to use inner classes right? since we only use the node class inside the linked list.
Inner classes are great for private data classes when you are pre java14
Please do some on streams
Can you talk about the builder pattern while you are on this topic? If I recall correctly inner class is used to build an object of a class using the builder pattern. You thoughts?
Unfortunately watched it in 1.25 and all i understood is Outerclass, innerclass, so innerclass outerclass interclass. LOL JK. I understood it perfectly bro thank you! 🖤
I love you John
Can you please tell me what is the Usecases, I mean when we should use innerclass?
Can you import a static inner class? I'm asking because I came into Java from Kotlin, and am still learning some of the 8-11-isms.
It doesn't matter that the static definition for a class is that it cannot instantiate objects because we saw that it can. What is the true definition of a static class?
I know this is months late, but I am curious. Can you return a method local internal class object from its local method scope? I’m thinking as like a really weird (and honestly not well designed) object factory method?
if john didn't exist i would be so lost
Can you help me with name of color theme which you use in video
Ahh my daily bread 🍞
This Video explains how to use inner classes well, but what problem do they solve ? When should I use them ?
can anybody tell me whats the theme he is using for eclipse and how to download it!
If I am running my program from a Terminal these nested classé are the only way my code works. If I put classes in different files compiler can't find them, even though If I run that exact same code in an IDE it works. Yes, I am a beginner.
Wouldn't the best use case for inner classes be to make them private and instantiated only by the class within which they reside? Kind of like a local utility class for the class.
Thks for your videos, John, they are very useful. One thing I did not understand about inner classes.. Can you please clarify if when we are using System.out.println we are using a inner class and how is this defined ? It always confused me the way we did the print out on java, instead calling a simple method of a system class, it seems we are using a kind on inner class. Is that correct?
(I know I'm a year late, but better late than never)
System.out simply returns a PrintStream which is another class / object. This method then has the println method. This is very similar to how the builder pattern works
Could i ask for a video about composition??
Off topic, but there appears to be something wrong with the subtitles/CC for this video, and I wanted to point it out! There's only an auto-generated Vietnamese option, and when translating the Vietnamese subtitles to English, it's completed unrelated to the video/what's actually being said. No idea what happened there, but I wanted to say something because it doesn't appear anybody else has!!
Can you please do GUI in java tutorial
The video was great, thank you :-) May God bless you. 1) I know that if you compile the outside class it will be ==> OuterClass.class and OuterClass$InnerClass.class 2) When you want to run it from the command line, do you have to put it in a jar file first? What does this jar file have to look like "inside" so that the entry point is set up correctly? 3) Lastly...how do you run this from the command line and JShell with these scenarios: a)with arguments, without arguments, and b)if using Eclipse...with arguments(Run Configurations set up), without arguments(Run Configurations turned off/cleared out). I've been having trouble with turning "off" Run Configurations lately. After some research I had some trouble understanding this in stackoverflow, sorry. Thanks for reading.
Hi Mr.jhon thank you for this video
but i want to ask you to talk leisurely if you can 🌝
I think that inner classes are used mostly as private or protected so thay can be instantiated within the code of the outer class. Using the inner classes outside of the code of the outer class is totally weird.
niceeee
Sound’s Weird but What is the use of inner class I never used it before ??
Hey, is anyone aware of the Eclipse theme that's being used in this tutorial? Thanks in advance :)
Q. What sort of problems are most easily solved using inner classes or method level classes?
Is it even good practice to instantiate an inner class outside the outer class? I thought the whole purpose of the Inner class was for it to be used inside the Outer class.
Where and why we use inner class
Interesting, but could have a bit more depth, like the proper use of inner data classes in a service class, or whatever.
John, although it seems you are using eclipse (as I do) for your IDE; I bserved that you did not wrote System.out.println when code, instead it seems you type a shorcut (as sysout) and the IDE expand it to the correct syntaxt System.out.println command. Is that correct ? What pluggin or utility are you using for that ? It would be great if you do a video with those kind of tricks to get the most of your IDE!
No plug-in required! It's built into Eclipse. You can just type "sysout" then Ctrl+Enter to autocomplete.
I also have a full video of Eclipse shortcuts that might be just what you're looking for!
ruclips.net/video/LIGkIGdmHII/видео.html
🖤
Good evening, may I ask what code editor do you use? Thanks
Sure! In this video I'm using Eclipse, but in more recent videos (and in my day to day coding) I use IntelliJ.
@@CodingWithJohn Thank you so much!
Did I miss the part of telling why to use Inner classes ?
😍😍😍😍😍😍😍
Never used a non static inner class in all my more than 20 years of java...
I have a doubt which most of them don't recognise whether such thing is present or not.
I know about all the types of Inner classes, came here just for that one doubt which is...
While creating the NON-STATIC Inner object using the Outer object as below
Outer.Inner inner=outer.new Inner();
As we know that the Inner class is non-static, and in order to access the Inner class's constructor, we are using the Outer class object, but how come we are using the Outer class name for accessing Inner class name( Outer.Inner inner ) as in above which should not be possible!!(as the class is non-static, which must be accessed only through the Outer class object) so we have to use the object of the Outer class to access the Inner class object like
( outer.Inner inner= outer.new Inner(); ) which should be the correct answer but is wrong!!?
Please anyone answer this.
above question in simple terms....
We are accessing outer non static data member( InnerClass ) inside static method( main method)directly . How is it possible?
***Outer.Inner inner*** = outer.new Inner();
need jsp servlet videos