Instance members and static members
25 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Each student has their own name and marks, but the school name is the same for everyone. Some data belongs to one object, and some to the whole class.
Instance members: one copy per object
An instance member is a field or method declared without static. It belongs to an object: every object gets its own copy of each instance field, and an instance method runs on one particular object.
Aarav and Priya each have their own marks. A field written without static is an instance field: every new Student(...) gets a fresh copy, so changing one object's field never touches another's.
Example 1 · Each object has its own instance fields
class Student { String name; int marks;
Student(String name, int marks) { this.name = name; this.marks = marks; }}
public class InstanceDemo { public static void main(String[] args) { Student s1 = new Student("Aarav", 85); Student s2 = new Student("Priya", 92); s1.marks = 90; System.out.println(s1.name + ": " + s1.marks); System.out.println(s2.name + ": " + s2.marks); }}Aarav: 90 Priya: 92
What this code does
s1ands2are two objects, and each has its ownnameandmarks.- Changing
s1.marksto 90 leaves Priya's marks at 92.
Fields and methods without static are instance members. To use one you need an object in front, s1.marks or s1.show(), exactly as in Create objects with new (opens in a new tab).
Static members: one copy for the whole class
A static member is a field or method declared with the static keyword. It belongs to the class itself, not to any object, so there is only one copy, shared by all objects and used with the class name.
Every student of a college shares its name, and storing it in each object lets the copies go out of sync. Write static String college;: now there is one college, kept with the class, and every object reads that same copy.
Example 2 · One static field shared by all objects
class Student { static String college = "IIT Madras"; String name;
Student(String name) { this.name = name; }
void show() { System.out.println(name + " - " + college); }}
public class SharedDemo { public static void main(String[] args) { Student s1 = new Student("Aarav"); Student s2 = new Student("Priya"); s1.show(); s2.show(); Student.college = "IIT Bombay"; s1.show(); s2.show(); }}Aarav - IIT Madras Priya - IIT Madras Aarav - IIT Bombay Priya - IIT Bombay
What this code does
- There is one
collegefor the whole class, so one change withStudent.collegeshows up in both objects. - Inside the instance method
show(), plaincollegemeans the static field.
Example 3 · A static field exists before any object
class Shop { static int gstPercent = 18; String item;}
public class NoObjectDemo { public static void main(String[] args) { System.out.println("GST: " + Shop.gstPercent + "%"); Shop.gstPercent = 12; System.out.println("New GST: " + Shop.gstPercent + "%"); }}GST: 18% New GST: 12%
What this code does
- No
Shopobject is created, yetShop.gstPercentcan be read and changed. - A static field lives with the class, so it needs no object. The instance field
itemwould need one.
Java creates that one copy when it first loads the class, before any object exists. So even with zero objects, Shop.gstPercent can be read and changed (Example 3).
The memory picture
After new Student("Aarav", 85) and new Student("Priya", 92), the heap holds two objects, each with its own name and marks. The static college is not inside either object: it is stored once, with the class.
Inside an instance method such as show(), plain name means this object's own field, while plain college leads to the one shared copy.
Reaching a member: object or class name
Rohan's ticket should be free, so someone writes t1.fare = 0;. Reach a static member with the class name, Ticket.fare; only an instance member needs an object, like t1.passenger.
Example 4 · Changing a static field through an object
class Ticket { static int fare = 50; String passenger;
Ticket(String passenger) { this.passenger = passenger; }}
public class ThroughObjectDemo { public static void main(String[] args) { Ticket t1 = new Ticket("Rohan"); Ticket t2 = new Ticket("Diya"); t1.fare = 0; System.out.println(t2.passenger + " pays Rs. " + t2.fare); }}Diya pays Rs. 0
What this code does
t1.fare = 0;looks like it changes only Rohan's fare, butfareis static: there is only one.- So Diya's ticket shows Rs. 0 too. Writing
Ticket.fare = 0;would make this effect clear.
Java accepts t1.fare, but it misleads the reader: it looks like Rohan's own fare, yet it is the one shared field. Inside the class's own methods, plain fare is enough. The reverse, Student.name for an instance field, does not compile, because the class name does not say which student.
Instance or static: how to choose
Ask one question about each field: is this value different for each object? If yes, it is an instance field. If it is one value for the whole class, make it static.
Example 5 · Mistake: a per-object value made static
class Player { static String name;
Player(String name) { Player.name = name; }
void show() { System.out.println("Player: " + name); }}
public class StaticNameBugDemo { public static void main(String[] args) { Player p1 = new Player("Rohit"); Player p2 = new Player("Virat"); p1.show(); p2.show(); }}Player: Virat Player: Virat
What this code does
- There is only one
namefor the class, so creating Virat overwrote Rohit. - A name is different for each player, so it must be an instance field: remove
staticand writethis.name = name;.
Name, marks, roll number and balance differ per object, so they are instance fields. A college name, a GST rate or a count of objects made is one value for all, so it is static. Each snippet below gets this choice wrong.
Find the bug
Find the instance or static mistake
Select the line that is wrong.
This should print Kavya.
Round 1 of 3static does not mean "cannot change". A static field can be changed; it just has one copy. Values that never change are covered in final variables and constants (opens in a new tab).Next step
Check your understanding
12 quick questions