Class definition in Java - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we define a class in Java, we want to know how much time it takes to create and use it.
We ask: How does the work grow when we make more objects from this class?
Analyze the time complexity of the following code snippet.
public class Box {
int length;
int width;
public Box(int length, int width) {
this.length = length;
this.width = width;
}
public int area() {
return length * width;
}
}
This code defines a simple Box class with a constructor and a method to calculate area.
Look for any repeated actions when using this class.
- Primary operation: Creating an object calls the constructor once.
- How many times: Each object creation runs the constructor once; area() runs once per call.
Think about making more boxes and calling area on each.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 constructor calls + 10 area calculations |
| 100 | 100 constructor calls + 100 area calculations |
| 1000 | 1000 constructor calls + 1000 area calculations |
Pattern observation: The work grows directly with the number of objects created and methods called.
Time Complexity: O(n)
This means if you create n boxes and call area on each, the total work grows in a straight line with n.
[X] Wrong: "Defining a class itself takes a lot of time as n grows."
[OK] Correct: Defining a class is done once and does not depend on n; only creating objects and calling methods depends on how many times you do it.
Understanding how object creation and method calls scale helps you write efficient code and explain your reasoning clearly in interviews.
"What if the area() method had a loop inside? How would the time complexity change?"
Practice
class Car { }Solution
Step 1: Understand the role of a class
A class defines a template or blueprint for creating objects that hold data and actions.Step 2: Match the definition to options
A blueprint to create objects with data and actions correctly describes a class as a blueprint for objects.Final Answer:
A blueprint to create objects with data and actions -> Option AQuick Check:
Class = blueprint for objects [OK]
- Confusing class with variable
- Thinking class is a method
- Mixing class with loops
Person in Java?Solution
Step 1: Recall Java class syntax
In Java, a class is defined using the keywordclassfollowed by the class name and braces.Step 2: Check each option
class Person { } matches the correct syntax:class Person { }. Others have incorrect order or keywords.Final Answer:
class Person { } -> Option CQuick Check:
Correct class syntax = class Name { } [OK]
- Swapping 'class' and class name
- Using '=' sign in class definition
- Using wrong keywords like 'define'
class Dog {
String name = "Buddy";
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
System.out.println(d.name);
}
}Solution
Step 1: Understand object creation and field access
The code creates a Dog object and accesses its field 'name' which is set to "Buddy".Step 2: Predict the printed output
Printing d.name outputs the string "Buddy" stored in the object.Final Answer:
Buddy -> Option AQuick Check:
Object field value = Buddy [OK]
- Expecting class name instead of field value
- Thinking uninitialized fields print 'null'
- Confusing syntax causing compile errors
class Animal {
String type;
void Animal() {
type = "Mammal";
}
}Solution
Step 1: Check constructor syntax
Constructors in Java must not have a return type, not even void.Step 2: Identify the error
The methodvoid Animal()is treated as a regular method, not a constructor, causing no constructor defined.Final Answer:
Constructor has void return type -> Option BQuick Check:
Constructor = no return type [OK]
- Adding void to constructor
- Thinking semicolon needed after field
- Believing class names must be lowercase
- Assuming fields must be static
Book with a field title and a method printTitle() that prints the title. Which code correctly implements this?Solution
Step 1: Check method to print field value
Method should use System.out.println with the field variabletitleto print its value.Step 2: Evaluate each option
Options printing the literal "title" (with or without newline) are incorrect. Calling undefinedprint(title)causes an error. OnlySystem.out.println(title)correctly prints the field value.Final Answer:
class Book { String title; void printTitle() { System.out.println(title); } } -> Option DQuick Check:
Print field with System.out.println(field) [OK]
- Using print() instead of println()
- Printing string literal instead of variable
- Calling undefined print() method
