Classes, Objects, and Methods
Class Syntax
"class" keyword use cheskoni classes ni create chestham java lo.
class ClassName{
// variables
// Methods
// objects
}
Variables Syntax ni previous articles lo discuss chesnam, go and check.
Methods
C language lo manam functions antam kadha, similarly in java manam Methods antam.
They define some behaviour or perform some task.
Methods Syntax
returnType FunctionName(datatype1 var1, datatype2 var2){
// body
}
returnType: Manam oka task or operation ni perform chesna tharvatha return chese value a datatype ni specify chesthado, dani manam return type antam.
void: we do not return any value.
int: Integer value return chestham.
String: String value return chestham.
Function can have 0 to n parameters or arguments.
Objects
Objects Syntax:
As we discussed oka specific class lo data ni access cheyali ante, we need create object.
ClassName ObjName = new ClassName();
"new" keyword use chesthe ah specific object memory lo create chesthunam ani ardam.
Epudu, Objects are main thing, objects use cheskoni verey class data ni access cheskuntam.
objName.OtherClassData -> (.) dot operator is used
Example:
class Student{
// 1st manam variables ni create cheskutunam
int rollno;
String name;
//Methods which does not return any value
void insertRecord(int r, String n){
rollno=r;
name=n;
}
void displayInformation(){
System.out.println(rollno+" "+name);
}
}
// Execution starts from main program
class TestStudent4{
public static void main(String args[]){
// Ekada manam student class kosam 2 students ni create chesthum (Objects)
Student s1=new Student();
Student s2=new Student();
// using (.) dot operator manam Student class lo methods ni access cheskutunam
s1.insertRecord(111,"Karan");
s2.insertRecord(222,"Aryan");
s1.displayInformation();
s2.displayInformation();
}
}
OUTPUT
111 Karan
222 Aryan
stack memory stores only variables of data address, actual data is stored in heap memory.
We have some specific keywords in OOP
static, this, super and some access modifiers which we will discuss in further articles.
Subscribe to my newsletter
Read articles from Kandadi Manasa directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by