AP® Computer Science A · Unit 3 · Topic 3.7
Class Methods and the final Keyword
A static method belongs to the class itself, not to any object, so it runs with no instance attached and can't touch instance variables or use this. A final variable can be assigned exactly once, and after that its value is frozen.
Every instance method runs on behalf of some object — when you call acct.deposit(50), Java hands deposit a hidden reference to acct, which is what this means inside the method. A static method has no such object. You call it on the class itself, like Math.√16, and nothing gets passed in as this. That's why a static method cannot read or write an instance variable sitting in its own class: instance variables live inside objects, one copy per object, and a static method isn't running inside any particular one. There is no object to reach through.
This is not a minor inconvenience you work around — it is a structural rule. A static method could be made to call an instance method only by first building an object and calling the method on that object, the same way any outside code would. Inside a static main, you cannot call an instance method bare; you write new BankAccount() and go from there. The keyword static genuinely changes what a method is allowed to do, not just how conveniently you can call it.
final works on variables the way static works on methods: it changes a capability. A final variable can be assigned once, and the compiler enforces that strictly — not once per run casually, but exactly one assignment, ever, whether at declaration or in a constructor. Try to assign it again and the code won't compile. Together, static and final describe two different kinds of locking: one locks a method away from objects, the other locks a variable's value in place once set.
Key terms
- static method
- A method that belongs to the class itself and runs with no object attached, so it has no this.
- instance variable
- A field that exists separately inside each object created from a class.
- final
- A keyword that restricts a variable to exactly one assignment, after which its value cannot change.
- this
- A hidden reference inside an instance method pointing to the object the method was called on.
Watch it explained
- How it worksHow does a class method run with no object attached, and what does that rule out?
- Worked exampleFind why a static main cannot call an instance method without building an object first.
- How it worksHow does final freeze a variable's value after the one assignment it is allowed?
- Common mistakeWhy isn't a static method just a normal method you can call more conveniently?
Check yourself
End of topic.