Java Tutorial for Beginners #18 - What Does STATIC Mean?

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

Key Takeaways

This Java tutorial for beginners explains the STATIC keyword and its usage with class variables and methods, covering object-oriented programming concepts and providing practical examples in Java.

Full Transcript

hey guys and welcome back to really Java programming tutorial so in today's video we're going to be going over kind of class variables and the keywords static so you may have seen that we've used static and a few things in like our main thing you're actually going to delete all this it says static right here and no one's really explained I guess I haven't really explained what static means and that's what I'm going to be doing in this video so let's get started and talk about class variables so right now we have something called attributes game so we have these two attributes which are specific to each instance of our dog so when we created multiple dog objects each dog have their own name and their own age and other dogs were not affected when we change one of the dog's name or one of the dog's age unless that dog was that mob viously right and that's great because we can have a ton of different values that are specific to different objects or different instances of this dog class now sometimes we want to have variables that will change or are the same for each instance so we can change them in one instance and if we do change it on one instance they will change in the other now those are known as class variables not instance variables because instance variables are like kind of attributes okay the way that we can create these class variables is we can do something like this yes it's not say protect it again say protected and by the way I'm just in my dog class I deleted a bunch of stuff out of it just to make a bit easier to work in and I still have the cat class but it's just I'm not using it for this video okay so we'll say protected static int count equals 0 now I know that that is a mouthful but what we're doing here essentially is we're creating a class variable which is an int named count equals 0 which is protected okay I know it's a lot of keywords but what is this static keyword do so what this is actually gonna do is it's gonna allow us to statically change count which means it's not gonna change for each instance it's only gonna change or sorry it'll change like all together right so what I was explaining before and that's what this static keyword is going to allow us to do so we don't actually care about what instance this variable is a part of we just care that it's a part of the dog class all right so if I want to change the value X well I do need still need to call this and I can say this dot count equals and then actually I'm gonna say plus equals one okay because what I want to do with this count here let's say static field dogs should be accessed in a static way okay let's just do this one second okay what I'm doing here sorry one second I'll explain this is I'm just gonna add 1 to this count because I want to keep track of how many dogs we have so that if I wanted to I don't know like see how many dogs have been created I could just simply call like dog count and then figure that out alright so what I'm doing here instead of calling this dot count which does actually work is I'm calling dog calm because since this is a static variable or static yeah static variable class variable whatever you want to call it it doesn't actually care about what instance I'm changing it on because it's gonna change for all the instances so I can actually use the name of the class to change it and that's why I gave me that little yellow line and not sure if you guys read that it said we should do this in a static way because well it's a static variable so I could however call like this dot count all right and that would work fine because this was an instance of the dog class so it knows where to look for the variable count but it's much better to do dog count like plus equals one okay now I'm going to prove this to you that this does indeed actually work I'm not just making things up so let's create some dog objects here I'd say a 10 or dog 10 equals new Wow typing is not great today new dog and we'll just say tune and so nine for age and we'll just copy this and what should we do let's do Bill maybe say Bill and that is new dog named bill age 10 okay now since this static variable here is protected I can actually access it from main so what I'm gonna do to change counting are to print count out first of all is I'll prove to you that system system dot out dot println I keep thinking we're in Python here and I'll just simply print out dog dot count like this okay and just see what we get as a value first okay so we get two so we already know that this is indeed working because well when we added one to count if it was this different for each one then it would just be one but anyways and if I want to change dog count what I can do is you can say dog dot count let's say equal is like seven okay and that's fine that works I print out dog Hut count we get seven now I'll show you - if I do like Tim dog count this will give us the same answer so you can see we get seven even though in Tim we didn't do anything with Tim to change account since it's a dog part of the dog class it has this count variable and it's changing obviously within Tim as well okay so that's kind of how the class variables work now what time to talk about static methods okay this was a static variable or class variable I like it's called class variable just cuz that's what I'm used to but you can call it whatever you want now we're gonna talk about static methods and what that is so it's very similar to this kind of the way that this works but is a little different so I'm gonna do here I'm gonna take public static void display okay and in here all I'm going to do is somebody system dot out dot println and I'll just print I am a dog okay so what this is doing now is I'm saying static void display now in here what's gonna happen essentially is we don't need an instance to call this so I can actually just call this on the the dog class so I can say like dog dot display like this and since it's a static method this works fine and this one Dean say I am a dog now as opposed to if this was a regular method so let's just make a regular method to kind of do a comparison here so let's say public void and we'll say display - okay and we'll literally - take this and put it word for word in here just to show you the difference so now watch what happens if I try to do dog don't display - you can see that we're getting a red line and saying that we need to make this static because we need an instance to call display - meaning that we need to have first created dog object and then we can use that object to call displayed to or we can do like this dot display to and that works fine because that's calling it on the instance right because this represents the instance that we're using so the static is gonna allow us to call it just simply using dog but the thing with static is it has no way to access our what he called our values or our attributes right so if I wanted to access like a dog's name or a dog's age well I can't do that from a static method like I can't type like this dog age that just simply doesn't work because if you think about it well what what what instance are we calling it on there's no instance we're just simply calling it on the dog class so we have no idea what age to look for or what name to look for so the only time we use static methods is when we don't care about the instance and we just want to do something and we want to store it within that class you can almost think of it as a function if you if you know other programming languages but that is ideally house static works so whenever you want to build a call something and you don't care about the like the instance you don't care what the attributes you can make it static and that way you can call it without having an instance so yeah so hopefully that makes sense to you guys I'll quickly go over what this void does one more time in case anyone forgets remember in our functions we can return values when you put void all this does is say we're not returning anything we're just gonna do something right so we could be like manipulating an array or in this case printing something up to the screen okay or maybe getting like input from the user but we're just not returning any value back to the call statement so anyways as always if you guys have any questions please make sure to leave a comment down below or join my discord server and 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 tutorial I explain what the Java keyword STATIC means and how we use it with class variables and methods. 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! ************************************************************** 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 - Java beginner tutorial - Static keyword java - Java static keyword - What does static mean 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 Java tutorial explains the STATIC keyword and its usage with class variables and methods, covering object-oriented programming concepts and providing practical examples in Java. The tutorial is designed for absolute beginners with no prior programming experience. By the end of the tutorial, viewers will understand how to use the STATIC keyword to create and manipulate class variables and methods.

Key Takeaways
  1. Create a class variable using the static keyword
  2. Change the value of a class variable using the static keyword
  3. Call a class variable using the class name
  4. Create a static variable in a class
  5. Call a static method on a class
  6. Access a static variable from a static method
💡 The STATIC keyword in Java allows a variable or method to be shared across all instances of a class, making it useful for storing and manipulating data that is common to all instances.

Related AI Lessons

Up next
Salesforce Flow New Features (Summer '26) | Open Record, URL & Show Toast Messages
AITECHONE
Watch →