Java: Modifiers
Quick summary
- static - not instantiated – belongs to the class not the object; any changes affect every instance.
- abstract - can’t be instantiated; can be extended only.
- final - can’t be subclassed or modified. (used with constants)
- void- returns nothing
- [default, concrete] - can be instantiated
Eclipse examples
- Eclipse example: static_fields_methods
- Eclipse example: final_fields_methods
- Eclipse example: final_class
- Eclipse example: abstract_classes_methods
See also topic Abstract methods.
See also Access modifiers.
Static
If your method is static, it cannot use instance variables that aren’t also static. Static stuff must all be static. Otherwise, you introduce change where it’s marked as non-changing.
Think of “stasis” – unchanging. No matter if you instantiate a class that has static members, the members don’t change.
The static keyword in Java means that the variable or function is shared between all instances of that class as it belongs to the type, not the actual objects themselves. So if you have a variable: private static int i = 0; and you increment it ( i++ ) in one instance, the change will be reflected in all instances. What does the ‘static’ keyword do in a class? Stack Overflow
Abstract
If you mark a class as abstract
, it can’t be instantiated. Only concrete
classes can be instantiated.
Final
From Stackoverflow.com
Final keyword has a numerous way to use:
- A final class cannot be subclassed.
- A final method cannot be overridden by subclasses
- A final variable can only be initialized once
Method overriding
Good resource: Java Method Overriding Examples and Concepts: Overriding Rules
About Tom Johnson
I'm an API technical writer based in the Seattle area. On this blog, I write about topics related to technical writing and communication — such as software documentation, API documentation, AI, information architecture, content strategy, writing processes, plain language, tech comm careers, and more. Check out my API documentation course if you're looking for more info about documenting APIs. Or see my posts on AI and AI course section for more on the latest in AI and tech comm.
If you're a technical writer and want to keep on top of the latest trends in the tech comm, be sure to subscribe to email updates below. You can also learn more about me or contact me. Finally, note that the opinions I express on my blog are my own points of view, not that of my employer.