Lecture 1-Class Objects
Lecture 1-Class Objects
class <class_name>{
field;
method;
}
Instance Variable in Java
➢ A variable which is created inside the class but outside the method is
known as an instance variable.
➢ Instance variable doesn't get memory at compile time.
➢ It gets memory at runtime when an object or instance is created.
➢ That is why it is known as an instance variable.
Method in Java
➢ In Java, a method is like a function which is used to expose the behavior of
an object.
➢ Advantage of Method
○ Code Reusability
○ Code Optimization
New Keyword in Java
➔ The new keyword is used to allocate memory at runtime.
➔ All objects get memory in Heap memory area.
Constructor in Java
● In Java, a constructor is a block of codes similar to the method.
● It is called when an instance of the class is created.
● At the time of calling constructor, memory for the object is allocated in the
memory.
Object and Class Example: main within
the class
In this example, we have created a Student class which has two data members
id and name. We are creating the object of the Student class by new keyword
and printing the object's value.
Here, we are creating a main() method inside the class.
File: Student.java
Object and Class Example: main within
the class
//Java Program to illustrate how to define a class and fields
//Defining a Student class.
class Student{
//defining fields
int id;//field or data member or instance variable Output:
String name; 0
//creating main method inside the Student class Null
null
public static void main(String args[]){
//Creating an object or instance
Student s1=new Student();//creating an object of Student
//Printing values of the object
System.out.println(s1.id);//accessing member through reference variable
System.out.println(s1.name); }
}
Object and Class Example: main outside
the class
In real time development, we create classes and use it from another class. It is
a better approach than previous one. Let's see a simple example, where we are
having main() method in another class.
We can have multiple classes in different Java files or single Java file. If you
define multiple classes in a single Java source file, it is a good idea to save the
file name with the class name which has main() method.
File: TestStudent1.java
Object and Class Example: main outside
the class
//Java Program to demonstrate having the main method in
//another class
//Creating Student class.
class Student{
Output:
int id;
0
String name;
null
}
//Creating another class TestStudent1 which contains the main method
class TestStudent1{
public static void main(String args[]){
Student s1=new Student();
System.out.println(s1.id);
System.out.println(s1.name); }
}
3 Ways to Initialize Object
There are 3 ways to initialize object in Java.
1. By reference variable
2. By method
3. By constructor
1) Object and Class Example: Initialization through
reference
Initializing an object means storing data into the object. Let's see a simple example
where we are going to initialize the object through a reference variable.
File: TestStudent2.java
class Student{
int id;
String name;
}
class TestStudent2{ Output:
public static void main(String args[]){
Student s1=new Student(); 101 Sonoo
s1.id=101;
s1.name="Sonoo";
System.out.println(s1.id+" "+s1.name);//printing members with a white space
}
}
1) Object and Class Example: Initialization through
reference (Cont.)
We can also create multiple objects and store information in it through reference variable.
File: TestStudent3.java
class Student{
int rollno; class TestStudent4{
String name; public static void main(String args[]){ Output:
void insertRecord(int r, String n){ Student s1=new Student();
111 Karan
rollno=r; Student s2=new Student();
name=n; s1.insertRecord(111,"Karan"); 222 Aryan
} s2.insertRecord(222,"Aryan");
void displayInformation() s1.displayInformation();
{System.out.println(rollno+" "+name);} s2.displayInformation();
} }
}
3) Object and Class Example: Initialization through a
constructor
We will learn about constructors in Java later.
Object and Class Example: Employee
Let's see an example where we are maintaining records of employees.
File: TestEmployee.java
class Employee{ public class TestEmployee {
int id; public static void main(String[] args) {
String name; Employee e1=new Employee(); Output:
float salary; Employee e2=new Employee(); 101 ajeet 45000.0
void insert(int i, String n, float s) { Employee e3=new Employee();
id=i; e1.insert(101,"ajeet",45000); 102 irfan 25000.0
name=n; e2.insert(102,"irfan",25000);
103 nakul 55000.0
salary=s; e3.insert(103,"nakul",55000);
} e1.display();
void display(){System.out.println(id+" e2.display();
"+name+" "+salary);} e3.display(); }
} }
References
1. https://www.javatpoint.com/object-and-class-in-java
2. https://en.wikipedia.org/wiki/Java_virtual_machine#:~:text=A%20Java%20virtual
%20machine%20
(JVM,required%20in%20a%20JVM%20implementation.
3. https://www.javatpoint.com/java-constructor
Thank You