Inner Class Java Tutorial - Creating and Using Inner Classes

Поділитися
Вставка
  • Опубліковано 8 січ 2025

КОМЕНТАРІ • 143

  • @CodingWithJohn
    @CodingWithJohn  3 роки тому +63

    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 >_>

  • @samuvisser
    @samuvisser 3 роки тому +121

    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

  • @johntravolta2655
    @johntravolta2655 2 роки тому +4

    Poland Computer Science student says thank you !!
    The way you teach is absolutely awesome!

  • @dilln2158
    @dilln2158 3 роки тому +24

    This mans gonna get me through university

  • @jamesdrinkard2751
    @jamesdrinkard2751 2 роки тому +2

    I've written a lot of different inner classes, but I still learned some things from the video. Kudos to you John!

  • @vikastiwari1297
    @vikastiwari1297 2 роки тому

    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.

  • @CharlesWhitford
    @CharlesWhitford 5 місяців тому

    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.

  • @aprendeguitarraclasica
    @aprendeguitarraclasica 3 роки тому +3

    You just made me interstand this concept after days of frustration. Thanks a lot!

  • @KingSardius
    @KingSardius Рік тому

    I learn something new with you every time I revisit familiar subjects. You are great!

  • @dragosudrea6533
    @dragosudrea6533 Рік тому +1

    >Has never seen this being used
    >Still makes a video about it
    Big hustler energy!

  • @Theinatoriinator
    @Theinatoriinator 2 роки тому +2

    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.

  • @virajgawand7133
    @virajgawand7133 Рік тому

    exactly 2 years later I'm watching this, watched last year for the first time actually :)

  • @jlwonders
    @jlwonders Рік тому

    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.

  • @stephenweber33
    @stephenweber33 2 роки тому +1

    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.

  • @faheemsyed9481
    @faheemsyed9481 2 роки тому +1

    John your style of teaching is simply fabulous. you can make learn a person easily. thanks John.

  • @jgabrielmiranda
    @jgabrielmiranda 8 місяців тому

    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.

  • @fc6827
    @fc6827 Рік тому

    Appreciate this explanation about static inner classes, for some reason I keep forgetting what makes it static. Great vid!

  • @DaxterSnickers
    @DaxterSnickers 2 роки тому +1

    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.

  • @Enanram
    @Enanram 2 роки тому +2

    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.

  • @jvsnyc
    @jvsnyc 3 роки тому +5

    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.

  • @OgedeiKhanOKOK
    @OgedeiKhanOKOK 2 роки тому +2

    Best thing I have learned from this channel is to never be lazy when typing names of stuff 🤣

  • @fredericoamigo
    @fredericoamigo 2 роки тому +1

    Dude, you’re amazing at explaining Java. Thank you so much for these vids!

  • @felixmorin4611
    @felixmorin4611 2 роки тому

    I think i just found the way to solove my problem i have with my code after 10 hs of searching, thank you!

  • @qwerrewq7388
    @qwerrewq7388 8 місяців тому

    This Video just saved me in my Exam, thank you for the great Video

  • @AndresGorostidi
    @AndresGorostidi 2 роки тому

    ¡Gracias!

  • @vladimirguevara3813
    @vladimirguevara3813 3 роки тому +7

    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.

  • @Sollace
    @Sollace 2 роки тому

    3:37 Oh wow, you learn something new every day XD

  • @shariefyassin6419
    @shariefyassin6419 2 роки тому +1

    I like how simple you make things and explain them , very useful video 👍👍

  • @kaledbrahmi3442
    @kaledbrahmi3442 3 роки тому +18

    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

  • @_Anna_Nass_
    @_Anna_Nass_ 2 роки тому

    Leaving a comment for the UA-cam algorithm because you’re super helpful 😁

  • @PieMongo
    @PieMongo 3 роки тому +3

    Hey you did it! Thank you so much!

    • @CodingWithJohn
      @CodingWithJohn  3 роки тому +1

      Hope it helps, thanks for watching!

    • @PieMongo
      @PieMongo 3 роки тому

      @@CodingWithJohn it sure does, keep up the good work!

  • @JonasKeil
    @JonasKeil 2 роки тому +1

    This tutorial is great John!!!

  • @MzamoHerbza
    @MzamoHerbza 2 роки тому

    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

  • @HeisenbergHK
    @HeisenbergHK 2 роки тому

    That was just GREAT!
    You didn`t waste my time!

  • @deepsuryavanshi6256
    @deepsuryavanshi6256 2 роки тому

    Thank You very much john, ur teaching style is soooooo awesome I got the info which I was searching for . Hatss offff....
    👍👍🙏

  • @ΧρηστοςΜελκιδης

    This guy about to save my semeseter

  • @redcrafterlppa303
    @redcrafterlppa303 3 роки тому

    Node in LinkedList and Pair in Map are great examples for local inner classes

  • @coding8453
    @coding8453 2 роки тому

    Thanks so much ❤️, now I got what was Map.Entry

  • @Devivl
    @Devivl 2 роки тому

    Great lesson, John! Thanks!

  • @TheIronWorkshop
    @TheIronWorkshop 3 роки тому

    Thanks john, you are a life saver!

  • @IndellableHatesHandles
    @IndellableHatesHandles 2 роки тому

    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.

  • @Youtuber-2024-lambda
    @Youtuber-2024-lambda 3 роки тому +1

    This is always a pleasure to Watch your vidéos courses, thank you very much John for all your java code passion !

  • @DiverseSituation
    @DiverseSituation 3 роки тому

    this was so clear! thanks Johnny, from Noga and Elai

  • @2Blu2
    @2Blu2 7 місяців тому

    Thank you soo much, u made this so easy omgggg!!

  • @bww705
    @bww705 2 роки тому

    Amazing thanks, you videos are the best!

  • @johndrowned7170
    @johndrowned7170 Рік тому +1

    I love you John

  • @LeighHughesSenlis
    @LeighHughesSenlis 2 роки тому +12

    I think the vast majority of your explanations are very good indeed, but not in this case. What needs to be explained first here is not how you create these inner classes, nor what different kinds there are, but what the point of them is. If you need to do something, why not just write another method? Why create an inner class, then write the method you need inside that? Please, explain the "why" first.

    • @CharlesWhitford
      @CharlesWhitford 5 місяців тому +1

      I think he alluded to the "why" towards the very end of the video -- he mentioned inner classes are not normal and are only used in large programmes, not in simple programmes. I understand your point about why he doesn't go into depth about the "why", but it's probably difficult to answer that question without giving an actual example of a long programme which has an inner class within an outer class. You can't do that in a 9-minute video.

    • @kcseanbrixd.calinao6700
      @kcseanbrixd.calinao6700 4 місяці тому +1

      I think the purpose of learning inner classes is so you can better understand how an anonymous class work.

    • @officialdreamplayz
      @officialdreamplayz 15 днів тому

      True

  • @dilln2158
    @dilln2158 3 роки тому +3

    You should do a java data structures series

  • @ssentongohenryatanus4917
    @ssentongohenryatanus4917 Рік тому

    Hello John!!! I am truly grateful the great work your doing.. Please help me with concerning serialization, deserialization and externalisation. Thank you

  • @johncusack4730
    @johncusack4730 4 місяці тому +1

    You're the best !

  • @jasper5016
    @jasper5016 2 роки тому

    You are born to teach. Can you please cover design patterns specially SOLID if possible?

  • @prannoyroy5312
    @prannoyroy5312 2 роки тому +6

    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?

  • @adams2736
    @adams2736 3 роки тому +1

    One thing missing in this video, explaining why would anyone use inner classes.

  • @MrMiltersen
    @MrMiltersen 2 роки тому

    Hi John!
    Love you videos!
    Would you maybe consider making a few videos about Spring? :)
    Kind regards!

  • @blehblah1
    @blehblah1 2 роки тому +1

    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.

  • @srikanthchitturi7283
    @srikanthchitturi7283 2 роки тому +1

    Thanks, I am a big fan of you!! Can you give some real time examples for better understanding..

  • @punitgupta7175
    @punitgupta7175 2 роки тому +1

    Can you please tell me what is the Usecases, I mean when we should use innerclass?

  • @wristdisabledwriter2893
    @wristdisabledwriter2893 3 роки тому +5

    I want an example of when it’s more useful to do the inner class than a extended class

    • @CodingWithJohn
      @CodingWithJohn  3 роки тому +20

      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.

    • @wristdisabledwriter2893
      @wristdisabledwriter2893 3 роки тому +5

      @@CodingWithJohn thank you. That makes a lot of sense

    • @francksgenlecroyant
      @francksgenlecroyant 3 роки тому

      Thank you, i am learning a lot from you John

    • @alfredomenezes8814
      @alfredomenezes8814 2 роки тому +1

      @@CodingWithJohn Perfect example! I was struggling to grasp some real use cases for inner classes, thank you very much

  • @andrealencar8060
    @andrealencar8060 2 роки тому

    Nice tutorial

  • @mykolazhabko3228
    @mykolazhabko3228 3 роки тому

    Thanks for tutorial! Simple and clear! Do you have a video explaining interfaces?? How can I use them in game development??

    • @CodingWithJohn
      @CodingWithJohn  3 роки тому +5

      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.

    • @mykolazhabko3228
      @mykolazhabko3228 3 роки тому

      @@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.

  • @HeyzonDub
    @HeyzonDub 9 місяців тому

    Thank you so much ❤

  • @larryd9577
    @larryd9577 2 роки тому

    Inner classes are great for private data classes when you are pre java14

  • @abulka
    @abulka Рік тому

    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.

  • @BekTesfa
    @BekTesfa 2 роки тому

    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?

  • @yuvaraj9740
    @yuvaraj9740 Рік тому

    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! 🖤

  • @teaadvice4996
    @teaadvice4996 2 роки тому

    if john didn't exist i would be so lost

  • @joshuamodiba853
    @joshuamodiba853 2 роки тому +1

    Ahh my daily bread 🍞

  • @smrtfasizmu6161
    @smrtfasizmu6161 3 роки тому

    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.

  • @leetaeryeo5269
    @leetaeryeo5269 3 роки тому

    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?

  • @MsRebeca4
    @MsRebeca4 5 місяців тому

    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?

  • @TheAnonymmynona
    @TheAnonymmynona 2 роки тому

    This Video explains how to use inner classes well, but what problem do they solve ? When should I use them ?

  • @tsalVlog
    @tsalVlog 3 роки тому

    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.

  • @pabloraindogarcia8107
    @pabloraindogarcia8107 2 роки тому +1

    Could i ask for a video about composition??

  • @AndresGorostidi
    @AndresGorostidi 2 роки тому

    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!

    • @CodingWithJohn
      @CodingWithJohn  2 роки тому

      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!
      ua-cam.com/video/LIGkIGdmHII/v-deo.html

  • @squ1shshr00m
    @squ1shshr00m Рік тому

    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!!

  • @zeppelinmexicano
    @zeppelinmexicano Рік тому

    Q. What sort of problems are most easily solved using inner classes or method level classes?

  • @vasifsultanov4308
    @vasifsultanov4308 2 роки тому

    Can you help me with name of color theme which you use in video

  • @RajasthaniINAmerica
    @RajasthaniINAmerica Рік тому

    Sound’s Weird but What is the use of inner class I never used it before ??

  • @vladimirkraus1438
    @vladimirkraus1438 9 місяців тому

    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.

  • @Shva927
    @Shva927 Рік тому

    thank u very much !

  • @MTB_Bay_Area
    @MTB_Bay_Area 2 роки тому

    Thank you

  • @chudchadanstud
    @chudchadanstud Рік тому

    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.

  • @Livinsomehow
    @Livinsomehow Рік тому

    can anybody tell me whats the theme he is using for eclipse and how to download it!

  • @AndresGorostidi
    @AndresGorostidi 2 роки тому

    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?

    • @pixlent
      @pixlent Рік тому

      (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

  • @mahe7524
    @mahe7524 2 роки тому

    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.

  • @anuraggdeshpande286
    @anuraggdeshpande286 2 роки тому

    Please do some on streams

  • @harigs72
    @harigs72 5 місяців тому

    6:35

  • @agent5420
    @agent5420 2 роки тому

    Can you please do GUI in java tutorial

  • @DarkOceanShark
    @DarkOceanShark 2 роки тому

    Hey, is anyone aware of the Eclipse theme that's being used in this tutorial? Thanks in advance :)

  • @kawan5851
    @kawan5851 Рік тому

    6:55 I think job interviews are totally broken. Most of the time you are being judged on those topics that are seldomly used in everyday professional programming.

  • @Gianzero
    @Gianzero 2 роки тому

    Good evening, may I ask what code editor do you use? Thanks

    • @CodingWithJohn
      @CodingWithJohn  2 роки тому

      Sure! In this video I'm using Eclipse, but in more recent videos (and in my day to day coding) I use IntelliJ.

    • @Gianzero
      @Gianzero 2 роки тому

      @@CodingWithJohn Thank you so much!

  • @study-me1oe
    @study-me1oe 2 роки тому

    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.

    • @study-me1oe
      @study-me1oe 2 роки тому

      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();

  • @durgaprasadsurada4643
    @durgaprasadsurada4643 3 роки тому

    Where and why we use inner class

  • @maramhasri163
    @maramhasri163 3 роки тому

    Hi Mr.jhon thank you for this video
    but i want to ask you to talk leisurely if you can 🌝

  • @stefans6557
    @stefans6557 2 роки тому

    Interesting, but could have a bit more depth, like the proper use of inner data classes in a service class, or whatever.

  • @sureshmanne7245
    @sureshmanne7245 3 роки тому

    Did I miss the part of telling why to use Inner classes ?

  • @tristanmoller9498
    @tristanmoller9498 2 роки тому

    Inner classes make sense, when you want a class A to implement the iterator interface. To effectively do so, you'd need an iterator object. The way that item iterates over objects from class A totally depends on what class A is. You'd probably need to implement the iterator differently for another class B. Here, encapsulation provides a use case for inner classes.

    • @kanishknishar
      @kanishknishar 2 роки тому

      But that would just be implementing the iterator interface.

  • @shubhamchouksey9904
    @shubhamchouksey9904 7 місяців тому

    @CodingWithJohn You mentioned about all types of inner classes. You forgot about Anonymous inner class. Maybe you don't want to cover it here. Thank you

  • @feynthefallen
    @feynthefallen 10 місяців тому

    Never used a non static inner class in all my more than 20 years of java...

  • @mateenmehmood5675
    @mateenmehmood5675 3 роки тому +1

    Nice. but you just explained how to create inner classes but not explained why we created it?