You’ve probably noticed it on your main methods already, but what does it really mean, this mysterious word static? Get an overview of why you need static and the different ways it can be used.
- So, what is this mysterious word, static? You've probably noticed it on your main methods already. In English, static means that something is fixed or stationary. In Java, it means that it's fixed to a class so that it can't be personalized by any instances of that class. For example, say we have a static variable laptop that's part of a programmer class. All programmer instances have to share the same static laptop, so when the laptop's edited by any of the programmer instances, it changes for all of them. You can put the static keyword in front of methods or variables, or you can have static blocks of code. If it's in front of a method, it means that you don't need an instance to invoke it. But also, you can't have access to any of the instance variables because it's attached to the class itself. It lives outside of the world of instances. It's like a barren planet where there's no life to interact with. You've already used static with the main method. The main method needs to be static because it's the starting point for Java, and at the very beginning, there are no instances around. No objects have been created yet. You'll likely use it with other methods that don't require access to instance data when the inputs of the method are all it needs to do its work. This is an idea that a lot of students have trouble with, so be patient with yourself and take some time to practice with it. Soon it will be much less mysterious.
This course was created by Madecraft. We are pleased to host this content in our library.
- What is an object in Java?
- Building Java classes
- Retrieving and updating variables with getters and setters
- Using the static keyword
- Understanding inheritance
- Diagramming code with UML
- Implementing polymorphism