Theory
6000 books, દરેક 5 variables
Unit 1 માં, BookBridge એ એક book ને loose variables માં store કર્યું હતું: title, section, copies, price, available.
college library લગભગ 6000 books ધરાવે છે. Unit 1 style ને follow કરતા, એ 30000 variables છે, title47 ને copies47 સાથે connecting કંઈ નથી સિવાય કે તમારી naming માં discipline.
એક book ના facts together belong કરે છે. Java ની unit of togetherness એ class છે, અને આ lesson finally BookBridge ને તેનું Book આપે છે.
Theory
index card template
જૂની libraries દરેક book માટે index card રાખતી હતી: એક printed template (Title: ___, Copies: ___) અને હજારો filled-in cards.
class એ printed template છે: એ list કરે છે કે દરેક book પાસે કયા facts છે, પણ એ itself કોઈ book નથી. object એ એક filled card છે: એક real book માટે real values. template ને once print કરવું અને many cards ને stamp કરવું એ exactly class Book once, new Book() many times છે.
Theory
Class અને field, ઔપચારિક રીતે
class એ user-defined blueprint છે જે data અને તેના પર work કરતી methods ને group કરે છે. data slots ને fields (અથવા instance variables) કહેવામાં આવે છે.
class Book {
String title;
int copies;
}
object ને new સાથે create કરો, તેના fields ને dot operator સાથે reach કરો:
Book b1 = new Book();
b1.title = "Let Us C";
BCA304 veterans માટે note: Java માં class ના closing brace પછી semicolon નથી; C++ એ એક demanded કર્યું હતું.
Practical
એક template થી બે cards
class Book {
String title; // fields: દરેક Book ને તેનું own મળે છે
int copies;
}
public class BookBridge {
public static void main(String[] args) {
Book b1 = new Book();
b1.title = "Let Us C";
b1.copies = 5;
Book b2 = new Book();
b2.title = "Java Complete Reference";
b2.copies = 2;
b2.copies = b2.copies - 1; // ફક્ત b2 ની એક copy issue કરો
System.out.println(b1.title + " | " + b1.copies);
System.out.println(b2.title + " | " + b2.copies);
}
}
Theory
દરેક object તેના fields ને own કરે છે
listing ને run કરો: b1 હજુ 5 copies show કરે છે, b2 1 show કરે છે. b2 ની copy issue કરવાથી ફક્ત b2 ના copies field ને touch કર્યું.
એ rule છે: દરેક new fresh, independent set of instance fields ને allocate કરે છે. class એ once load થાય છે; data પ્રતિ object ગુણાકાર થાય છે.
એટલે "3 Books create કર્યા પછી કેટલા title variables exist કરે છે?" નો precise answer છે: 3, દરેક object ની અંદર એક, plus class itself માં 0.
Quiz
BookBridge એ 3 Book objects create કરે છે. memory માં title field ની કેટલી separate copies exist કરે છે?
- 1, બધા Book objects દ્વારા shared
- 3, દરેક object ની અંદર એક
- 0 જ્યાં સુધી title ને value assign ન થાય
- કયા constructor ને use કરવામાં આવ્યો તેના પર depend કરે છે
Show the answer
3, દરેક object ની અંદર એક
Instance fields objects ને belong કરે છે, એટલે 3 objects એટલે 3 independent title slots; એક ને change કરવાથી બીજા ને ક્યારેય disturb નથી થતા. Option A static field ને describe કરે છે, જે તમે આ unit માં later meet કરશો. Option C assignment ને allocation સાથે confuse કરે છે: slot એ new ની moment થી exist કરે છે, default value hold કરતા. Constructors (option D) initial values ને decide કરે છે, કેટલા fields exist કરે છે તે નહીં.
Think first
field જે તમે ક્યારેય set નથી કર્યું
Book b = new Book(); System.out.println(b.copies); તમે ક્યારેય copies assign નથી કર્યા. tap કરતા પહેલા: error, garbage value, અથવા કંઈક બીજું?
Show the answer
એ 0 print કરે છે. Java દરેક field ને automatic default આપે છે: numeric types માટે 0, boolean માટે false, String જેવા references માટે null (એટલે b.title null print કરે છે). બે contrasts worth marks: C એ તમને garbage આપ્યું, અને Java ની methods ની અંદર local variables ને કોઈ default મળતું નથી; એક ને assign કરતા પહેલા use કરવું compile error છે. Fields defaults સાથે born થાય છે, locals empty born થાય છે.
Watch out
Blueprint એ building નથી
class Book { ... } લખવાથી books માટે કોઈ memory allocate નથી થતી. Students લખે છે "defining a class creates an object" અને mark lose કરે છે: class એ ફક્ત plan છે; fields માટે memory દરેક new પર appear થાય છે.
બીજો trap: Book b; alone reference ને declare કરે છે જે કંઈ પર point નથી કરતું (null). b = new Book(); પહેલા b.title ને touch કરવું compile fine થાય છે અને પછી runtime પર NullPointerException throw કરે છે, Java નો most famous crash.
Theory
card ને next front desk મળે છે
હમણે કોઈપણ code કોઈપણ card પર scribble કરી શકે છે: b1.copies = -50; happily compile થાય છે. real library તેના records ને guard કરે છે: તમે change ને request કરો છો અને system rules ને apply કરે છે. એ guarding એ next 2 lessons છે: access controls decide કરે છે કે કોણ field ને touch કરી શકે છે, અને constructors make sure કરે છે કે કોઈ Book અડધું-ભરેલું born ન થાય. BCA304 નું CanteenTill same path ને follow કર્યું: data first, protection second.
Summary
Key takeaways
- Class related data (fields) અને behaviour ને એક blueprint માં group કરે છે; object એ તેનું એક live instance છે.
- class Book { String title; int copies; } template ને define કરે છે; new Book() fresh object ને stamp કરે છે.
- દરેક object દરેક instance field ની independent copy ને own કરે છે.
- Fields automatic defaults મેળવે છે (0, false, null); local variables ને કોઈ મળતું નથી.
- Declared પણ unassigned reference null છે; એને use કરવાથી NullPointerException throw થાય છે.
- Java માં class brace પછી semicolon નથી, C++ થી અલગ.
- Memory hook: class એ printed card છે, new fresh copy ને stamp કરે છે.