Core Java · Chapter 13 of 48
The static keyword
By the end of this chapter you can keep one value for a whole class, write methods that need no object, make constants, and decide who may build your objects. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Objects
- 6 tasks
- Java
- In the all labs plan
The first 2 chapters of Core Java are free. This one comes with the all labs plan.
Why you need this
Anjali has a complaint from Mumbai: two customers at CSMT both got token 14, from two different counters, and both ran for the same chai. Meera wants one sequence for the whole day: SBC gives out 1, NDLS gives out 2, SBC gives out 3, and no two tokens ever share a number.
Every field you have written so far is an instance field: each object gets its own copy. So each counter kept its own count, and each one started at 1. You could pass one shared counter object into every place that issues tokens, but miss one place and the duplicates are back.
What you build: 6 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Token.java: number tokens from one shared counter
In Token.java, make next(station) count one more token in the shared static counter issued, then return a new Token for that station whose number is the new count. Numbers run across every station: the first token of a day is 1, and each call gets the next number, whatever the station.
For example, at the start of a day next("SBC") is token 1 at SBC, then next("NDLS") is token 2 at NDLS, and next("SBC") is token 3 at SBC.
- Task 2
Token.java: count today's tokens and start a new day
In Token.java, make issuedSoFar() return how many tokens all stations together have been given since the last reset, and make resetForNewDay() set that count back to 0, so the next token issued is number 1 again.
For example, after resetForNewDay(), issuedSoFar() is 0. After two calls to next it is 2. After another reset, next("NDLS") is token 1 and issuedSoFar() is 1. The check issues its tokens with next, from task 1.
- Task 3
Token.java: print a token's label
In Token.java, make label() return this token's station, a dash, and its number padded with zeros to three digits.
For example, token 7 at SBC is SBC-007, token 42 at CSMT is CSMT-042, and token 123 at SBC is SBC-123. The check makes its tokens with next, from task 1.
- Task 4
Token.java: a real GST constant, used by withGst
In Token.java, make GST_PERCENT a constant: static and final, still 5. Then make withGst(amount) return the amount in rupees plus GST_PERCENT percent of it, rounded down to whole rupees with int arithmetic.
For example, withGst(40) returns 42, withGst(25) returns 26 (1.25 rounds down to 1), withGst(100) returns 105 and withGst(0) returns 0.
- Task 5
Token.java: make the constructor private
In Token.java, make Token's constructor private, so code outside the class cannot build a token with any number it likes, and next(station) is the only way to get one. next still works, because it is inside the class.
For example, Token.next("SBC") still returns a token at SBC, while new Token("SBC", 99) in another class no longer compiles.
- Task 6 · interview question
Interview: Token.java, predict which classes compile
In Token.java, make predictCompiles() return four booleans, one for each small class in its comment, in order (TokenA, TokenB, TokenC, TokenD): true if javac compiles that class, false if it does not.
The check compiles the same four classes and compares your list with what javac really said.
What you will learn
- Static fields: one copy for the whole class, shared by every object.
- Static methods and factories: methods called on the class, with no object, including one that builds objects behind a private constructor.
- Constants: static final values that never change.
- Static versus instance: which code runs on an object, and which does not.
More in Objects
Build The static keyword yourself
Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Core Java are free. This one comes with the all labs plan.