Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Static members vs instance members
📖 Scenario: Imagine you are creating a simple program to manage a library. You want to keep track of the total number of books in the library as well as details about each individual book.
🎯 Goal: You will build a class Book that has both static and instance members. The static member will count the total books, and the instance members will store details about each book. You will then display the total number of books and details of each book.
📋 What You'll Learn
Create a class Book with instance members Title and Author.
Add a static member TotalBooks to count how many books have been created.
Increment TotalBooks each time a new Book object is created.
Create at least two Book objects with different titles and authors.
Print the total number of books using the static member.
Print the details of each book using instance members.
💡 Why This Matters
🌍 Real World
Tracking shared data like total counts or settings across many objects is common in software like inventory systems or user management.
💼 Career
Understanding static vs instance members is essential for writing clean, efficient, and maintainable object-oriented code in professional C# development.
Progress0 / 4 steps
1
Create the Book class with instance members
Create a class called Book with two public instance string members: Title and Author. Do not add any static members yet.
C Sharp (C#)
Hint
Instance members belong to each object. Use public string Title; and public string Author; inside the class.
2
Add a static member to count total books
Inside the Book class, add a public static integer member called TotalBooks initialized to 0. Also add a constructor that takes title and author as parameters, sets the instance members, and increments TotalBooks by 1.
C Sharp (C#)
Hint
Static members belong to the class itself. Use public static int TotalBooks = 0;. The constructor sets instance members and increases the static count.
3
Create Book objects and use members
In the Main method, create two Book objects: one with title "1984" and author "George Orwell", and another with title "Pride and Prejudice" and author "Jane Austen". Store them in variables book1 and book2.
C Sharp (C#)
Hint
Use new Book("1984", "George Orwell") to create the first book and assign it to book1. Do the same for book2.
4
Print total books and details of each book
Print the total number of books using Book.TotalBooks. Then print the title and author of book1 and book2 using their instance members.
C Sharp (C#)
Hint
Use Console.WriteLine to print the static member TotalBooks and the instance members Title and Author of each book.
Practice
(1/5)
1. Which statement correctly describes a static member in C#?
easy
A. It belongs to the class and is shared by all instances.
B. It belongs to each object and stores unique data.
C. It can only be accessed through an object instance.
D. It is created every time a new object is instantiated.
Solution
Step 1: Understand static member concept
Static members belong to the class itself, not to any individual object.
Step 2: Compare with instance members
Instance members belong to objects and hold unique data per object, unlike static members.
Final Answer:
It belongs to the class and is shared by all instances. -> Option A
Quick Check:
Static member = shared by class [OK]
Hint: Static = shared by class, instance = unique per object [OK]
Common Mistakes:
Confusing static with instance members
Thinking static members are unique per object
Believing static members require object to access
2. Which of the following is the correct way to declare a static field in a C# class?
easy
A. public static int count;
B. public int static count;
C. public int count;
D. int static public count;
Solution
Step 1: Recall C# static field syntax
The correct order is access modifier, then 'static', then type and name.
Step 2: Check each option
public static int count; matches the correct syntax: 'public static int count;'. Others have wrong order or missing keywords.
Final Answer:
public static int count; -> Option A
Quick Check:
Access modifier + static + type + name [OK]
Hint: Use 'public static' before type for static fields [OK]
Common Mistakes:
Placing 'static' after the type
Omitting 'static' keyword
Incorrect order of keywords
3. What will be the output of the following C# code?
class Counter {
public static int count = 0;
public Counter() {
count++;
}
}
class Program {
static void Main() {
Counter c1 = new Counter();
Counter c2 = new Counter();
Console.WriteLine(Counter.count);
}
}
medium
A. 0
B. 2
C. 1
D. Compilation error
Solution
Step 1: Understand static field behavior
The static field 'count' is shared by all instances and starts at 0.
Step 2: Trace constructor calls
Each new Counter() increments 'count' by 1. Two objects created, so count becomes 2.
Final Answer:
2 -> Option B
Quick Check:
Static count increments twice = 2 [OK]
Hint: Static fields keep shared state across all objects [OK]
Common Mistakes:
Thinking count resets per object
Confusing instance and static fields
Expecting compilation error due to static access
4. Identify the error in this C# code snippet:
class Example {
public static int value = 10;
public int GetValue() {
return value;
}
}
medium
A. Cannot access static field 'value' inside instance method.
B. Method GetValue must be static to access static field 'value'.
C. Static field 'value' must be private.
D. No error; code compiles and runs correctly.
Solution
Step 1: Check static field access rules
Instance methods can access static fields directly without error.
Step 2: Verify code correctness
Method GetValue returns static field 'value' correctly; no syntax or access errors.
Final Answer:
No error; code compiles and runs correctly. -> Option D
Quick Check:
Instance method can access static field [OK]
Hint: Instance methods can access static fields directly [OK]
5. You want to count how many objects of a class are created, but also keep a unique ID for each object starting from 1. Which approach correctly uses static and instance members?
hard
A. Use only instance fields for both counter and ID.
B. Use an instance counter incremented in constructor; assign its value to a static ID field.
C. Use a static counter incremented in constructor; assign its value to an instance ID field.
D. Use static fields for both counter and ID without instance fields.
Solution
Step 1: Understand counting objects with static field
A static counter shared by all objects can track total created objects.
Step 2: Assign unique ID per object using instance field
Each object gets its own instance ID assigned from the static counter value.
Final Answer:
Use a static counter incremented in constructor; assign its value to an instance ID field. -> Option C
Quick Check:
Static counter + instance ID = unique IDs [OK]
Hint: Static counts total; instance stores unique ID per object [OK]