Skip to content

Commit d985518

Browse files
committed
feat: Implement auto-generated student roll numbers using static counter and date-based prefix
WHAT the code does: - Defines StudentName class: - Private field rollNo stores the unique identifier for each student. - Static counter count starts at 1 and increments with each new object. - Private method assignRollNo(): - Uses java.util.Date to fetch the current year. - Constructs roll number as "University-<year>-<count>". - Increments count for the next student. - Constructor assigns rollNo by calling assignRollNo(). - Public getter getRollNo() returns the roll number. - Defines main class ToughStaticSolveIt: - Creates three StudentName objects (s1, s2, s3). - Prints their generated roll numbers. WHY this matters: - Demonstrates **static variable usage** for generating unique sequential identifiers shared across all instances. - Shows how to combine static state with dynamic data (current year from Date) to generate meaningful roll numbers. - Ensures each student object gets a unique roll number without requiring manual input. HOW it works: 1. First StudentName object: - count = 1 → assigns "University-2025-1". - count increments to 2. 2. Second StudentName object: - count = 2 → assigns "University-2025-2". - count increments to 3. 3. Third StudentName object: - count = 3 → assigns "University-2025-3". - count increments to 4. 4. Printing confirms unique sequential roll numbers. Tips and gotchas: - java.util.Date.getYear() is deprecated; prefer java.time.LocalDate.now().getYear() for modern code. - Static counter resets only when JVM restarts; persistence across sessions would require external storage (file, database). - Class and method naming could follow Java conventions: - StudentName → Student - ToughStaticSolveIt → RollNumberGeneratorDemo - Making assignRollNo() private ensures roll numbers are controlled internally, enforcing encapsulation. Use-cases / analogies: - Similar to how universities or organizations auto-generate IDs for students, employees, or tickets. - Like a bakery numbering receipts: each new customer gets the next number, prefixed with the current year. Short key: java static-counter unique-id auto-generated roll-numbers encapsulation. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 8aeaf36 commit d985518

File tree

2 files changed

+7
-23
lines changed

2 files changed

+7
-23
lines changed

Section16StaticFinal/src/Car.java

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
import java.util.Date;
22

3-
class StudentName
4-
{
3+
class StudentName {
54
private String rollNo;
65

76
private static int count=1;
87

9-
private String assignRollNo()
10-
{
8+
private String assignRollNo() {
119
Date d = new Date();
1210

1311
String rno="University-"+(d.getYear()+1900)+"-"+count++;
1412

1513
return rno;
1614
}
17-
StudentName()
18-
{
15+
16+
StudentName() {
1917
rollNo=assignRollNo();
2018
}
21-
public String getRollNo()
22-
{
19+
20+
public String getRollNo() {
2321
return rollNo;
2422
}
2523
}
@@ -34,4 +32,4 @@ public static void main(String[] args) {
3432
System.out.println(s2.getRollNo());
3533
System.out.println(s3.getRollNo());
3634
}
37-
}
35+
}

0 commit comments

Comments
 (0)