Object-Oriented Programming Foundations
Method Parameters, Return Values, and Method Design
আপনি একটি free preview lesson দেখছেন।
Lesson Overview
একটি method object-এর behavior প্রকাশ করে।
এখন পর্যন্ত আমরা এমন methods দেখেছি যেগুলো কোনো input নেয় না:
enrollment.completeLesson();
কিন্তু বাস্তব application-এ methods-কে প্রায়ই caller থেকে information নিতে হয়।
Examples:
course.changeTitle(
"Java and OOP Foundation"
);
course.changePrice(
499_000L
);
enrollment.completeLessons(
3
);
একইভাবে, methods caller-এর কাছে একটি result ফেরত দিতে পারে।
double progress =
enrollment.calculateProgress();
boolean completed =
enrollment.isCompleted();
এই lesson-এ আমরা শিখব:
- Parameter এবং argument
- Multiple parameters
- Return type
returnstatementvoidmethods- Local variables এবং method scope
- Primitive values method-এ pass করা
- Object references method-এ pass করা
- Command এবং query methods
- Focused এবং predictable method design
Learning Objectives
এই lesson শেষ করার পর আপনি পারবেন:
- Parameter এবং argument-এর পার্থক্য ব্যাখ্যা করতে
- এক বা একাধিক parameterসহ method লিখতে
- Method থেকে value return করতে
voidএবং value-returning method-এর পার্থক্য বুঝতে- Returned value store, compare এবং reuse করতে
- Method scope বুঝতে
- Primitive argument pass করলে কী ঘটে তা ব্যাখ্যা করতে
- Object reference pass করলে কী ঘটে তা ব্যাখ্যা করতে
- Command এবং query method আলাদা করতে
- State change করার আগে input validate করতে
- Small এবং meaningful methods design করতে
Why Methods Need Parameters
ধরা যাক Course class-এর title change করার একটি method আছে।
void changeTitle() {
title =
"Java and OOP Foundation";
}
এই method-এর সমস্যা হলো title hard-coded।
এটি শুধু একটি নির্দিষ্ট title assign করতে পারে।
Methodটিকে reusable করতে caller থেকে নতুন title নেওয়া যায়।
void changeTitle(
String newTitle
) {
title = newTitle;
}
এখন caller প্রয়োজন অনুযায়ী title দিতে পারে।
course.changeTitle(
"Java and OOP Foundation"
);
অন্য course-এর জন্য:
course.changeTitle(
"Backend Development with Spring Boot"
);
Parameter method-কে different inputs নিয়ে একই behavior perform করতে দেয়।
Parameter and Argument
Parameter এবং argument closely related হলেও এক জিনিস নয়।
Parameter
Method declaration-এর মধ্যে যে variable লেখা হয়, সেটি parameter।
void changeTitle(
String newTitle
) {
title = newTitle;
}
এখানে:
String newTitle
একটি parameter।
Argument
Method call করার সময় যে actual value দেওয়া হয়, সেটি argument।
course.changeTitle(
"Java and OOP Foundation"
);
এখানে:
"Java and OOP Foundation"
একটি argument।
Parameter vs Argument
| Parameter | Argument |
|---|---|
| Method declaration-এ থাকে | Method call-এ থাকে |
| Input receive করার variable | Actual input value |
| Method-এর local scope-এর অংশ | Parameter-এর type-এর সঙ্গে compatible হতে হয় |
Example:
void changePrice(
long newPriceInPaisa
) {
priceInPaisa =
newPriceInPaisa;
}
Call:
course.changePrice(
499_000L
);
এখানে:
newPriceInPaisa → Parameter
499_000L → Argument
Declaring a Parameter
General syntax:
returnType methodName(
ParameterType parameterName
) {
// Method body
}
Example:
void changeLearnerName(
String newName
) {
learnerName = newName;
}
Parameter-এর type compiler-কে বলে method কী ধরনের input expect করছে।
Parameter Scope
Parameter শুধুমাত্র method-এর ভেতরে accessible।
void changeLearnerName(
String newName
) {
learnerName = newName;
}
newName method-এর বাইরে ব্যবহার করা যাবে না।
Wrong:
void displayName() {
System.out.println(
newName
);
}
এটি compile হবে না।
কারণ newName অন্য method-এর scope-এর অংশ।
Multiple Parameters
একটি method একাধিক parameter নিতে পারে।
void initializeProgress(
int completed,
int total
) {
completedLessons = completed;
totalLessons = total;
}
Call:
enrollment.initializeProgress(
10,
20
);
Arguments position অনুযায়ী parameters-এর সঙ্গে match করে।
10 → completed
20 → total
Argument Order Matters
ধরা যাক method:
void initializeProgress(
int completed,
int total
) {
}
Correct call:
enrollment.initializeProgress(
10,
20
);
Incorrect but compilable call:
enrollment.initializeProgress(
20,
10
);
দুটিই int, তাই compiler semantic mistake বুঝতে পারবে না।
এখন state হবে:
Completed lessons: 20
Total lessons: 10
এটি business-invalid।
এই কারণে:
- Parameter name meaningful হওয়া উচিত
- Method input validate করা উচিত
- একই type-এর অনেক parameter থাকলে design পুনর্বিবেচনা করা উচিত
Meaningful Parameter Names
Weak:
void update(
int x,
int y
) {
}
Better:
void updateProgress(
int completedLessons,
int totalLessons
) {
}
Parameter name value-এর business meaning প্রকাশ করা উচিত।
Parameter Validation
Caller যেকোনো value দিতে পারে।
Method-এর দায়িত্ব হলো invalid input object state-এ ঢুকতে না দেওয়া।
Example:
void changeTitle(
String newTitle
) {
if (
newTitle == null
|| newTitle.isBlank()
) {
return;
}
title =
newTitle.strip();
}
এই method reject করবে:
null
""
" "
এবং valid title store করার আগে surrounding spaces remove করবে।
Silent Failure-এর Limitation
আগের method invalid input হলে শুধু return করেছে।
if (
newTitle == null
|| newTitle.isBlank()
) {
return;
}
এতে object invalid হয় না, কিন্তু caller বুঝতে পারে না operation সফল হয়েছে কি না।
Simple learning example-এর জন্য এটি acceptable।
Production code-এ alternatives হতে পারে:
booleanresult return করা- Exception throw করা
- Validation result return করা
- Application boundary-তে error handle করা
Example:
boolean changeTitle(
String newTitle
) {
if (
newTitle == null
|| newTitle.isBlank()
) {
return false;
}
title =
newTitle.strip();
return true;
}
Caller:
boolean changed =
course.changeTitle(
"Java Foundation"
);
এখন caller operation result জানতে পারে।
void Methods
void method কোনো result value return করে না।
void completeLesson() {
completedLessons++;
}
Call:
enrollment.completeLesson();
Method object state change করছে, কিন্তু caller-এর কাছে কোনো value ফেরত দিচ্ছে না।
Value-Returning Methods
একটি method calculation বা query result caller-এর কাছে ফেরত দিতে পারে।
int calculateRemainingLessons() {
return totalLessons
- completedLessons;
}
Usage:
int remainingLessons =
enrollment
.calculateRemainingLessons();
এখানে method একটি int return করেছে।
Return Type
Method name-এর আগে return type লেখা হয়।
double calculateProgress() {
return completedLessons
* 100.0
/ totalLessons;
}
Return type:
double
Returned expression-ও double-এর সঙ্গে compatible হতে হবে।
The return Statement
return দুইটি কাজ করতে পারে:
- Method execution শেষ করা
- Caller-এর কাছে value ফেরত দেওয়া
Example:
double calculateProgress() {
if (totalLessons <= 0) {
return 0.0;
}
return completedLessons
* 100.0
/ totalLessons;
}
যদি totalLessons <= 0 হয়, method immediately 0.0 return করবে।
Remaining code execute হবে না।
Return Type Must Match
Wrong:
int calculateProgress() {
return completedLessons
* 100.0
/ totalLessons;
}
Expression-এর result double, কিন্তু return type int।
Correct:
double calculateProgress() {
return completedLessons
* 100.0
/ totalLessons;
}
Every Execution Path Must Return
Wrong:
boolean isCompleted() {
if (
completedLessons
== totalLessons
) {
return true;
}
}
Condition false হলে method কোনো value return করে না।
Correct:
boolean isCompleted() {
return totalLessons > 0
&& completedLessons
== totalLessons;
}
Returned Values Are Reusable
Method থেকে value return করলে caller resultটি বিভিন্নভাবে ব্যবহার করতে পারে।
Store
double progress =
enrollment.calculateProgress();
System.out.println(
enrollment.calculateProgress()
);
Compare
if (
enrollment.calculateProgress()
>= 80.0
) {
System.out.println(
"Strong progress"
);
}
Format
String progressText =
"%.2f%%".formatted(
enrollment
.calculateProgress()
);
Pass to Another Method
printProgress(
enrollment.calculateProgress()
);
এই flexibility-এর কারণে calculation method-এর ভেতরে direct print করার চেয়ে result return করা ভালো।
Boolean-Returning Methods
Boolean method সাধারণত একটি yes/no domain question answer করে।
boolean isCompleted() {
return totalLessons > 0
&& completedLessons
== totalLessons;
}
Usage:
if (enrollment.isCompleted()) {
System.out.println(
"Course completed"
);
}
Common boolean method prefixes:
is
has
can
should
Examples:
isCompleted()
hasAvailableSeats()
canEnroll()
shouldIssueCertificate()
Method name caller-এর প্রশ্নের মতো শোনায়।
Command and Query Methods
Method design বোঝার জন্য একটি useful distinction:
Command
Command method state change করে বা action perform করে।
void completeLesson() {
completedLessons++;
}
Query
Query method information return করে এবং ideally state change করে না।
double calculateProgress() {
return completedLessons
* 100.0
/ totalLessons;
}
Avoid Surprising Query Methods
Poor design:
double calculateProgress() {
completedLessons++;
return completedLessons
* 100.0
/ totalLessons;
}
Caller progress জানতে চেয়েছে।
কিন্তু method silently একটি lesson complete করে দিয়েছে।
এটি surprising side effect।
Better:
void completeLesson() {
completedLessons++;
}
double calculateProgress() {
return completedLessons
* 100.0
/ totalLessons;
}
Method name এবং behavior match করা উচিত।
Designing completeLessons()
আমরা একবারে multiple lessons complete করার method লিখতে পারি।
void completeLessons(
int lessonCount
) {
if (lessonCount <= 0) {
return;
}
int updatedCount =
completedLessons
+ lessonCount;
if (
updatedCount
> totalLessons
) {
return;
}
completedLessons =
updatedCount;
}
এই method তিনটি কাজ করছে:
- Input validate করছে
- Proposed new state calculate করছে
- Valid হলে state update করছে
Validate Before Mutating
Wrong:
void completeLessons(
int lessonCount
) {
completedLessons +=
lessonCount;
if (
completedLessons
> totalLessons
) {
return;
}
}
এখানে validation হওয়ার আগেই object invalid state-এ চলে গেছে।
Correct approach:
int updatedCount =
completedLessons
+ lessonCount;
if (
updatedCount
> totalLessons
) {
return;
}
completedLessons =
updatedCount;
Principle:
প্রথমে proposed state calculate করুন, তারপর validate করুন, সবশেষে commit করুন।
Local Variables in Methods
int updatedCount =
completedLessons
+ lessonCount;
updatedCount একটি local variable।
এটি temporary calculation hold করে।
Method execution শেষ হলে এটি object state হিসেবে থাকে না।
Fields:
completedLessons
totalLessons
Object state-এর অংশ।
Local variable:
updatedCount
Method implementation-এর temporary detail।
Java Passes Arguments by Value
Java সব arguments by value pass করে।
এই statement primitive values এবং object references—দুটোর জন্যই সত্য।
Passing Primitive Values
Example:
static void changeValue(
int value
) {
value = 100;
}
Call:
int number = 20;
changeValue(number);
System.out.println(number);
Output:
20
কারণ method number variable পায়নি।
Method primitive value 20-এর একটি copy পেয়েছে।
Method-এর local copy 100 হয়েছে।
Caller-এর number unchanged।
Primitive Passing Step by Step
int number = 20;
Call:
changeValue(number);
Conceptually:
number = 20
method parameter value = copy of 20
Inside method:
value = 100;
Final:
value = 100
number = 20
Passing Object References
Object-এর ক্ষেত্রে Java object itself copy করে না।
Object reference value-এর একটি copy method-এ pass করে।
static void completeOneLesson(
Enrollment enrollment
) {
enrollment.completeLessons(1);
}
Usage:
Enrollment nurEnrollment =
new Enrollment();
nurEnrollment.completedLessons = 10;
nurEnrollment.totalLessons = 20;
completeOneLesson(
nurEnrollment
);
System.out.println(
nurEnrollment.completedLessons
);
Output:
11
Caller reference এবং method parameter একই object access করছে।
তাই object mutation caller থেকে visible।
Reassigning a Reference Parameter
static void replaceEnrollment(
Enrollment enrollment
) {
enrollment =
new Enrollment();
}
Usage:
Enrollment original =
new Enrollment();
original.completedLessons = 5;
replaceEnrollment(original);
System.out.println(
original.completedLessons
);
Output:
5
Method তার local reference copy নতুন object-এর দিকে point করিয়েছে।
Caller-এর original variable change হয়নি।
Correct Mental Model
Object argument pass করলে:
Caller reference
│
└──> Enrollment object
Method parameter
│
└──> Same Enrollment object
দুটি reference value একই object point করে।
Method object mutate করতে পারে।
কিন্তু method parameter reassign করলে caller reference reassign হয় না।
Methods Can Receive Objects
Methods primitive বা String ছাড়াও domain objects নিতে পারে।
Conceptual example:
void enroll(
Learner learner,
Course course
) {
}
এটি multiple related values আলাদাভাবে pass করার চেয়ে clearer হতে পারে।
Weak design:
void enroll(
String learnerName,
String learnerEmail,
String courseTitle,
int totalLessons,
long coursePriceInPaisa
) {
}
এখানে data সম্ভবত Learner এবং Course objects-এর মধ্যে থাকা উচিত।
Better:
void enroll(
Learner learner,
Course course
) {
}
Object relationships composition lesson-এ বিস্তারিত শেখানো হবে।
Too Many Parameters
একটি method অনেক parameters নিলে call বোঝা কঠিন হয়।
void createEnrollment(
String learnerName,
String learnerEmail,
String courseTitle,
int totalLessons,
int completedLessons,
boolean active
) {
}
Possible problems:
- Argument order ভুল হতে পারে
- Method multiple responsibilities নিচ্ছে
- Related data-এর জন্য object missing
- Invalid combinations তৈরি করা সহজ
- Calls difficult to read
Too many parameters automatic error নয়।
কিন্তু এটি design reconsider করার signal।
Avoid Unclear Boolean Arguments
Example:
course.publish(true);
Call দেখে বোঝা যাচ্ছে না true কী বোঝায়।
Possible meanings:
- Notify learners
- Publish immediately
- Make course public
- Skip validation
Clearer methods হতে পারে:
course.publish();
অথবা:
course.publishAndNotifyLearners();
Boolean parameter ব্যবহার করা যায়, কিন্তু call-এর meaning obvious হওয়া উচিত।
Return Data Instead of Printing
Less reusable:
void displayRemainingLessons() {
System.out.println(
totalLessons
- completedLessons
);
}
Better:
int calculateRemainingLessons() {
return totalLessons
- completedLessons;
}
Caller presentation control করবে।
int remaining =
enrollment
.calculateRemainingLessons();
System.out.println(
"Remaining lessons: "
+ remaining
);
এতে domain logic এবং presentation আলাদা থাকে।
Method Names Should Reveal Intent
Good:
changeTitle()
completeLessons()
calculateProgress()
isCompleted()
Weak:
process()
handle()
update()
doWork()
Generic name method-এর purpose hide করে।
Method name action বা question clearly express করা উচিত।
Keep Methods Focused
Poor:
void completeLessonAndPrintProgressAndSendEmail() {
}
এই method একসঙ্গে করছে:
- State mutation
- Calculation
- Presentation
- Communication
Better separation:
enrollment.completeLesson();
double progress =
enrollment.calculateProgress();
Email sending অন্য component-এর responsibility হতে পারে।
একটি method ideally একটি clear level of responsibility-তে কাজ করবে।
Side Effects
Method-এর observable state change বা external interaction-কে side effect বলা হয়।
Examples:
completedLessons++;
Object state change করে।
System.out.println(
"Completed"
);
Console output produce করে।
repository.save(enrollment);
Database state change করে।
Side effects সবসময় খারাপ নয়।
কিন্তু method name এবং contract থেকে side effect predictable হওয়া উচিত।
Complete Example
Enrollment.java
public class Enrollment {
String learnerName;
String courseTitle;
int completedLessons;
int totalLessons;
boolean completeLessons(
int lessonCount
) {
if (lessonCount <= 0) {
return false;
}
int updatedCount =
completedLessons
+ lessonCount;
if (
updatedCount
> totalLessons
) {
return false;
}
completedLessons =
updatedCount;
return true;
}
int calculateRemainingLessons() {
return totalLessons
- completedLessons;
}
double calculateProgress() {
if (totalLessons <= 0) {
return 0.0;
}
return completedLessons
* 100.0
/ totalLessons;
}
boolean isCompleted() {
return totalLessons > 0
&& completedLessons
== totalLessons;
}
}
Main.java
public class Main {
public static void main(String[] args) {
Enrollment sakibEnrollment =
new Enrollment();
sakibEnrollment.learnerName =
"Sakib";
sakibEnrollment.courseTitle =
"Java and OOP Foundation";
sakibEnrollment.completedLessons = 15;
sakibEnrollment.totalLessons = 20;
boolean updated =
sakibEnrollment
.completeLessons(3);
if (!updated) {
System.out.println(
"Progress could not be updated."
);
return;
}
double progress =
sakibEnrollment
.calculateProgress();
int remainingLessons =
sakibEnrollment
.calculateRemainingLessons();
System.out.println(
"Learner: "
+ sakibEnrollment.learnerName
);
System.out.println(
"Progress: "
+ "%.2f%%".formatted(
progress
)
);
System.out.println(
"Remaining lessons: "
+ remainingLessons
);
System.out.println(
"Completed: "
+ sakibEnrollment.isCompleted()
);
}
}
Output:
Learner: Sakib
Progress: 90.00%
Remaining lessons: 2
Completed: false
Engineering Note: Method Contracts
একটি method-এর contract caller-কে জানায়:
- কী input valid
- Method কী state change করবে
- কী result return করবে
- Failure কীভাবে represent হবে
Current method:
boolean completeLessons(
int lessonCount
)
Contract:
Positive lesson count required
Updated count cannot exceed total
true means state changed
false means request rejected
Clear method contract debugging এবং testing সহজ করে।
Production systems-এ method failure behavior accidental হওয়া উচিত নয়।
Common Mistakes
Parameter এবং Argument Mix করা
Parameter:
int lessonCount
Argument:
completeLessons(3);
Wrong Argument Order
updateProgress(
totalLessons,
completedLessons
);
Compiler error নাও হতে পারে, কিন্তু business state wrong হবে।
Wrong Return Type
boolean calculateProgress() {
return 80.0;
}
Invalid, কারণ 80.0 একটি double।
Missing Return Path
boolean isCompleted() {
if (
completedLessons
== totalLessons
) {
return true;
}
}
False condition-এর জন্য return নেই।
Mutating Before Validation
completedLessons +=
lessonCount;
if (
completedLessons
> totalLessons
) {
return false;
}
Object ইতোমধ্যে invalid state-এ গেছে।
Assuming Primitive Parameter Changes Caller
Primitive parameter local copy।
Method-এর assignment caller variable change করে না।
Assuming Java Passes Objects by Reference
Java object reference value-এর copy pass করে।
Shared object mutate করা যায়।
Caller reference variable reassign করা যায় না।
Printing Instead of Returning
Method-এর মধ্যে calculation print করলে result reuse করা কঠিন হয়।
Generic Method Names
process()
handle()
update()
Context ছাড়া intent unclear।
Practice Exercises
Exercise 1: Change Course Title
Implement:
boolean changeTitle(
String newTitle
)
Rules:
nullreject করতে হবে- Blank value reject করতে হবে
- Valid title
strip()করে store করতে হবে - Success হলে
true - Failure হলে
false
Exercise 2: Change Course Price
Implement:
boolean changePrice(
long newPriceInPaisa
)
Negative price reject করুন।
Exercise 3: Complete Multiple Lessons
Implement:
boolean completeLessons(
int lessonCount
)
Rules:
- Count positive হতে হবে
- Updated count total-এর বেশি হতে পারবে না
- Validate করার পরে state change করতে হবে
Exercise 4: Primitive Passing
Predict করুন:
static void change(
int value
) {
value = 100;
}
int number = 25;
change(number);
System.out.println(number);
Result explain করুন।
Exercise 5: Object Reference Passing
একটি Enrollment object method-এ pass করুন।
Method object-এর একটি lesson complete করবে।
Confirm করুন caller updated state দেখতে পায়।
Exercise 6: Reference Reassignment
Method লিখুন:
static void replace(
Enrollment enrollment
) {
enrollment =
new Enrollment();
}
Caller-এর reference কেন replace হয় না explain করুন।
Exercise 7: Improve Method Design
Poor method:
void process(
int value,
boolean flag
) {
}
Course enrollment context-এ meaningful method name এবং parameters design করুন।
Exercise 8: Command or Query
নিচের methods classify করুন:
completeLesson()
calculateProgress()
cancel()
isCompleted()
changeTitle()
hasAvailableSeats()
Predict the Output
Question 1
static void change(
int value
) {
value = 50;
}
int number = 10;
change(number);
System.out.println(number);
Question 2
Enrollment enrollment =
new Enrollment();
enrollment.completedLessons = 5;
enrollment.totalLessons = 10;
boolean updated =
enrollment.completeLessons(3);
System.out.println(updated);
System.out.println(
enrollment.completedLessons
);
Question 3
Enrollment enrollment =
new Enrollment();
enrollment.completedLessons = 9;
enrollment.totalLessons = 10;
boolean updated =
enrollment.completeLessons(3);
System.out.println(updated);
System.out.println(
enrollment.completedLessons
);
Question 4
static void replace(
Enrollment enrollment
) {
enrollment =
new Enrollment();
enrollment.completedLessons = 50;
}
Enrollment original =
new Enrollment();
original.completedLessons = 5;
replace(original);
System.out.println(
original.completedLessons
);
Predict the Output Answers
Answer 1
10
Method primitive value-এর copy পরিবর্তন করেছে।
Answer 2
true
8
Update valid ছিল।
Answer 3
false
9
Requested update total lessons exceed করত।
Answer 4
5
Method শুধু local reference parameter reassign করেছে।
Knowledge Check
Question 1
Parameter এবং argument-এর difference কী?
Question 2
Parameter-এর scope কোথায়?
Question 3
void method কী return করে?
Question 4
return statement কী করে?
Question 5
Command method কী?
Question 6
Query method কী?
Question 7
Query method-এর hidden mutation problematic কেন?
Question 8
Primitive argument method-এ কীভাবে pass হয়?
Question 9
Object argument method-এ কীভাবে pass হয়?
Question 10
Reference parameter reassign করলে caller reference change হয় কি?
Question 11
State mutation-এর আগে validation কেন করা উচিত?
Question 12
Returned data direct printing-এর চেয়ে reusable কেন?
Question 13
অনেক parameters থাকা method কী indicate করতে পারে?
Question 14
Method contract কী?
Knowledge Check Answers
Answer 1
Parameter method declaration-এর input variable। Argument method call-এর actual value।
Answer 2
শুধু সেই method-এর ভেতরে।
Answer 3
কোনো result value return করে না।
Answer 4
Method execution শেষ করে এবং value-returning method হলে caller-এর কাছে result দেয়।
Answer 5
যে method state change করে বা action perform করে।
Answer 6
যে method state read করে বা information calculate করে।
Answer 7
Caller শুধু information expect করলেও object state unexpectedly change হয়ে যায়।
Answer 8
Primitive value-এর একটি copy pass হয়।
Answer 9
Object reference value-এর একটি copy pass হয়।
Answer 10
না।
Answer 11
Invalid state object-এর মধ্যে commit হওয়া prevent করতে।
Answer 12
Returned data store, compare, format, test এবং অন্য calculations-এ ব্যবহার করা যায়।
Answer 13
Method multiple responsibilities নিচ্ছে অথবা related values-এর জন্য একটি object missing হতে পারে।
Answer 14
Method কী input গ্রহণ করে, কী behavior perform করে, কী result দেয় এবং failure কীভাবে represent করে—তার clear expectation।
Lesson Summary
এই lesson-এ আমরা শিখেছি:
- Parameter method-কে input নিতে দেয়
- Argument method call-এর actual value
- Parameters method-local scope-এর অংশ
- Multiple arguments position অনুযায়ী match করে
- Meaningful names semantic mistakes কমাতে সাহায্য করে
voidmethods result value return করে না- Value-returning methods reusable information provide করে
returnmethod execution শেষ করে- Boolean methods domain questions answer করে
- Command methods state change করে
- Query methods information return করে
- Queries hidden state mutation করা উচিত নয়
- State mutation-এর আগে proposed state validate করা উচিত
- Local variables temporary calculations-এর জন্য useful
- Java সব arguments by value pass করে
- Primitive value copy হয়
- Object reference value-ও copy হয়
- Copied references একই object access করতে পারে
- Reference parameter reassign করলে caller reference change হয় না
- Returning data direct printing-এর চেয়ে reusable
- Too many parameters একটি design signal
- Boolean arguments call readability কমাতে পারে
- Clear method contract predictable behavior তৈরি করে
- Focused methods testing এবং maintenance সহজ করে
Next Lesson
পরবর্তী lesson:
Constructors and Valid Object Creation
আমরা শিখব:
- Constructor কী
- Object creation-এর সময় initialization
- Default constructor
- Parameterized constructor
- Required object state
- Constructor validation
- Partially initialized object prevent করা
- Constructor এবং method-এর difference
- Object-কে শুরু থেকেই valid state-এ তৈরি করা