Java Tutorial for Beginners #20 - Inner Classes

Tech With Tim · Beginner ·🛠️ AI Tools & Apps ·7y ago

Key Takeaways

This video tutorial covers the concept of inner classes in Java, including how to create and access them within other classes or methods. The tutorial provides examples of using inner classes, including creating instances of them and calling their methods.

Full Transcript

hey guys and welcome back to another Java programming tutorial so in today's video we are gonna be going over in our classes now inner classes are pretty pretty easy actually all they are is really just a class inside of another class or inside of a method and I'll show you some examples as we go along but since we already know all about classes and methods and static and whatnot I'm not gonna explain too much of it I'm just gonna kind of show you how we can create instances of inner classes in different ways so I'm gonna start by doing this inside an outer class here that I've already created you can see on the side here I'm just gonna create another class called interpose right now inner class is just gonna simply have one method in it and I'm sure you make this a public void display and in here all I'm going to do is simply just display similar to the screen so we know that we're actually in the right class here okay now let's say this is an inner class okay and what's our error here class at token hmm interesting must have made a mistake somewhere here one second guys oh sorry I put these two brackets here hmm don't know why out of those anyways okay so this is an inner class now outside of this class I'm gonna create another method that's gonna be a part of the outer class okay so this class is actually contained within the outer class meaning that currently since it's private we can only actually access it from within our class so if we want create an instance of this that means we must need a method in our class to do that so in this case I'm gonna say public void inner and in here we're going to take nothing and all we're gonna do is we're gonna create an instance of our inner class and just display okay so we're just gonna say inner class I n equals new in your class like that okay and then all we're gonna do is just call I end display and make sure that this is working okay so now our class is finished we have an inner class and we have this void inner and all that does is create an instance of our inner class and print it to the screen again note that this is private meaning we we access it from within our our class okay so now if I go to my main Java I can run a command so or whatever creating objects that we can see this so first of all what I have to do is create an outer class object so that we can call that inner class so outer class out equals you and an outer class now it is worth noting that I don't have any constructors here meaning that I don't need to give any information to my outer class from my inner class when I first call it and that's why I can just leave these blank brackets okay so now if I want to see my inner class what I have to do is I have to do out dot inner okay so we'll call that method to create the inner class and then run that so let's run quickly here and we can see we get this is an inner class awesome so that's working fine now I'm just gonna make this public now let me show you how we can do this from outside of the class so really now what we need to do is we need to create an instance of outer class and then we need to call the method inner to be able to create the inner class and that's because this was private but now that it's public we should be able to create an instance of it outside of the class so the way that we do that is we still need an instance of outer class because this class is defined with an outer class so we need to first create this so that this is kind of created right so we did that and we have outer class now what I can do is I can say our class dot inner class okay and we just call this one I add equals lower case equal is out which is going to be our instance of outer class dot and then in this case we're gonna strike inner class believe this is correct but I guess we'll see oh I probably need an equal sign and we'll type new new out inner class and let's see what our little red line is create class inner class in package out interesting one second guys okay so that's for the issue I knew it was something like this what we need to do is we need to do a new and then inter class because we're creating a new instance inside of this right of outer class of inner class okay so now what we've done is we've created this i n which is an object which is a new inner class so now we can simply use I n which is an instance of inner class right to display so now we can do a and display okay so if we run this see this is an inner class and that is working fine so those are two ways you can go to access the inner class now right now you might be like kay well why the heck would I do this because now it's just like a ton more lines well there's many times where you don't really care about like this instance and you just want an instance of the object inside of it so in that case this would make more sense to do and obviously you would most likely have more methods than just one that you're gonna be using okay all right so now that we have that I guess let's show creating this kind of inner class stuff so I'm going to delete this inside of a method so we can actually create these inside of methods which are really useful because sometimes within the method we want to use a class and we don't want to have to define like a new file for another class okay so we have this public void inner so I guess let's just work with this right now you know even leave in a class right now I'm gonna show you how this works so all I can do in here anything and say I want you do public you private in this case I'm just gonna say I'm gonna just say class let's see in class in a class and then here yes that works fine we're just gonna redefine display so just say public void display and now that I think of it I probably could have just copied out my other code but whatever get some practice typing println and then in here again well this should probably be a t we will just type inner class okay so this is going to work perfectly fine whenever we call this void inner then we have this class inner class we're gonna create an instance of it and we're going to display it so if I run this I guess wow I really should just kept all that code that's fine why is this not giving you the autocrat outer class out because new and out of class and brackets and then in this case we'll simply just call out dot inner ok so now we can see what should happen is again we should print inner class so there we go and we get inner class now pretty much you can do this like wherever you want the reason I'm not saying this is public or private is just because like it's already within this void so to use it we're gonna have to call this void so there's no really there's not really a point in putting like private or public but again like if you wanted to type public class inner class or oh actually saying you can't do that so I guess I was right the first time so since it's inside of this void you're only gonna be able to use when we call this so there's no route point and really giving it like a public or private method or class name sorry because in here we can still do public and private methods because we'll outside of the class we might be using those methods right so you can see obviously since I'm creating this instance inside of the the void so inside of you know and if I tried to do that outside of it it wouldn't work because this class is only defined when we call the inner method okay so I think that's kind of been it for inner methods there are like anonymous inner methods and some few a few like other cool things you can do with it but I don't find them particularly useful so I'm not going to teach them here especially in like a beginners tutorial but if you guys are interested in learning about them I will leave a link in the description just for any of you guys that are curious and as always if you enjoyed the video please make sure you leave a like and subscribe and I will see you again in another one [Music]

Original Description

In this java tutorial I talk about Inner classes. Inner classes can be placed within other classes or within methods. There are a few different types of inner classes and different ways to reference them which I discus sin the video. Java beginners programming tutorial. A complete java tutorial meant for absolute beginners. Absolutely no programming experience required. If you are new to programming and want to get started you are in the right place! Learn More About Inner Classes: https://www.tutorialspoint.com/java/java_innerclasses.htm ************************************************************** One-Time Donations: https://goo.gl/pbCE9J Support the Channel: https://www.patreon.com/techwithtim Join my discord server: https://discord.gg/pr2k55t ************************************************************** Please leave a LIKE and SUBSCRIBE for more content! Tags: - Tech With Tim - Tech With Tim Java - Java Tutorial for Beginners - Beginner Java Tutorials - Classes and Objects Java - Java Inner Classes - Classes in Java
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 0 of 60

← Previous Next →
1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This tutorial teaches how to create and use inner classes in Java, including how to access them from outer classes and methods. It provides examples and explanations to help beginners understand the concept.

Key Takeaways
  1. Create an outer class
  2. Define an inner class within the outer class
  3. Create an instance of the inner class
  4. Access the inner class from the outer class
  5. Use the inner class to organize code
💡 Inner classes can be used to organize code and provide a way to access and manipulate data within a class or method.

Related AI Lessons

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →