KwickAcademy Java · 6 min · free
Static Members: Class Variables and Class Methods
How static gives one shared copy for the whole class: class variables, class methods, the static context error, and when to use static.
Follows the syllabus of: ICSE Class 10 Computer Applications, ISC Class 11 Computer Science (868), ISC Class 12 Computer Science (868)
On screen in this lesson
Two kinds of variables
| Instance variable: each object gets its own copy |
| Class variable: marked static, one copy for all |
| The class variable belongs to the class itself |
| Read it as ClassName.variable |
Instance vs class variable
| Point | Instance variable | Class variable |
|---|---|---|
| Keyword | none | static |
| Copies | one per object | one for the class |
| Accessed by | object.name | ClassName.name |
| Created when | object is made | class is loaded |
static methods
| Belong to the class, not to an object |
| Call them as ClassName.method() |
| Can use static members directly |
| Cannot use instance members or this directly |
| main is static, so Java can start without an object |
Who can use what
| Written inside | Static member | Instance member |
|---|---|---|
| static method | directly | through an object |
| instance method | directly | directly |
| constructor | directly | directly |
When to use static
| A value that is the same for every object |
| A counter shared by all objects |
| A constant: static final double RATE = 7.1 |
| A helper method that needs no object data |
| Not for data that differs per object |
Quick recap
| Instance variable: one copy per object |
| static variable: one shared copy for the class |
| static method: call with the class name |
| static code cannot use instance members directly |
| Use static for shared values, counters and helpers |
Quick answers
Why is main static?
Java must start the program before any object exists.
Why does printing marks inside a static method fail?
marks belongs to an object, and a static method has no object, so Java cannot know whose marks to use.
KwickClips from this lesson
Short clips, one idea each. Good for revision the night before.
How many copies does a static variable have?40 sec
Can you call a method without an object?37 sec
Why does this not compile?40 sec
Should marks be static?44 secThe full lesson, in text
Hello students, welcome to Kwickprep. Every student in a school has a different name, but they all share one school name. If the school changes its name, should we update every student? Today we will learn static members, which solve exactly this problem.
A class can hold two kinds of variables. An instance variable gives every object its own separate copy, like each student's name. A class variable is marked with the word static, and there is only one copy, shared by all objects. That single copy belongs to the class itself, not to any one object. So we usually use it with the class name, a dot, and the variable name.
Let us draw the memory step by step. When the class is loaded, Java makes one box for the static variable school. Then the riya object is created, with its own box for name. The aman object is created next, with another separate box for name. Now, if school is changed once, both objects see the new value, because they point to the same box. But changing riya's name never touches aman's name.
Here school is static, and name is an instance variable. We create two objects, riya and aman. Then we change Stu dot school to Model School, only once. Pause and predict, what does aman's school print? It prints Model School, because there is only one shared copy.
Let us compare them side by side. An instance variable has no special keyword, but a class variable uses static. An instance variable has one copy per object, while a class variable has one copy for the whole class. We reach an instance variable through an object, and a class variable through the class name. An instance variable is created with each object, but a class variable is created when the class is loaded.
Methods can be static too, and they follow five rules. A static method, also called a class method, belongs to the class. So we call it with the class name, without making any object. It can use other static members directly. It cannot use instance variables or the keyword this directly, because no object is involved. This is also why main is static, since Java must start the program before any object exists.
Here cube is a static method of the class Calc. We call Calc dot cube of three, with no object, and it prints twenty seven. You already know another static method, Math dot square root. It prints seven point zero, because square root always returns a double.
Now a common mistake. Show is static, but marks is an instance variable. Which object's marks should it print? Java cannot know, so this does not compile. The error says, non-static variable marks cannot be referenced from a static context.
Here is the rule for mixing static and non static members. A static method can use static members directly, but instance members only through an object. An instance method can use both kinds directly, because it always runs on some object. A constructor is the same, since it is building an object.
This program gives roll numbers automatically. Count is static, so all students share one counter. Roll is an instance variable, so each student keeps their own number. The constructor adds one to count, and copies it into roll. Riya is made first and gets roll one, then a second student gets roll two. So riya's roll prints one, and count prints two.
So when should you use static? Use it for a value that is the same for every object, like the school name. Use it for a counter that all objects share. Use static final for a constant, like a fixed interest rate, where final means it cannot change. Use a static method for a helper that needs no object data, like cube or square root. Never use static for data that is different for each object, like a name or marks.
Let us revise what we learned today. An instance variable gives each object its own copy. A static variable has one copy, shared by the whole class. A static method is called with the class name, and needs no object. Static code cannot use instance members directly. Use static for shared values, counters, constants and helper methods.
Courses that teach this
| Course | Unit |
|---|---|
| ICSE Class 10 Computer Applications | User-Defined Methods |
| ISC Class 11 Computer Science (868) | Statements, Control Flow and Functions |
| ISC Class 12 Computer Science (868) | Functions (Methods) |
| Programming All levels Java | Classes, Objects and Methods |
Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.
Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

