Module-2 Notes(1st IA)
Module-2 Notes(1st IA)
Module-2
For Example, Pen is an object. Its name is Reynolds; the color is white, known as its
state. It is used to write, so writing is its behavior.
Object Definitions:
class <class_name> {
data member/s;
method/s;}
Method in Java
In Java, a method is like a function used to expose an object’s behavior.
Advantage of Method
Code Reusability
Code Optimization
Syntax
className object = new className();
// Example for Bike class object creation
Bike sportsBike = new Bike();
Bike touringBike = new Bike();
class Student {
int id;
String name; }
// Creating another class
class TestStudent{
public static void main(String args[]){
Student s1=new Student();
System.out.println(s1.id);
System.out.println(s1.name);
} }
class Student {
int id;
String name;
}
class TestStudent{
public static void main(String args[]){
Student s1=new Student();
s1.id=1;
s1.name="SrisailaNath";
//printing members with a white space
System.out.println(s1.id+" "+s1.name);
}
}
Output
1 SrisailaNath
class Student{
int rollno;
String name;
void ReadInfo(int r, String n){
rollno=r;
name=n;
}
void DispInfo(){
System.out.println(rollno+" "+name);}
}
class TestStudent{
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
s1. ReadInfo (1, "KrishnaNanda");
s2. ReadInfo (2, "NarayaniPriya");
s1. DispInfo ();
s2. DispInfo ();
} }
Output
1 KrishnaNanda
2 NarayaniPriya
Constructors 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 the constructor, memory for the
object is allocated in the memory.
It is a special type of method which is used to initialize the object. Every time an
object is created using the new() keyword, at least one constructor is called. It calls a
default constructor if there is no constructor available in the class. In such cases, the Java
compiler provides a default constructor by default.
Note: It is called a constructor because it constructs the values at the time of object
creation. It is not necessary to write a constructor for a class. It is because the java
compiler creates a default constructor if your class doesn't have any.
class Student{
int id;
String name;
void display(){System.out.println(id+" "+name);}
}
class StudentTest{
public static void main(String args[]){
Student3 s1=new Student3();
Student3 s2=new Student3();
s1.display();
s2.display();
} }
Output
0 null
0 null
In the above class, you are not creating any constructor so the compiler provides
you with a default constructor. Here 0 and null values are provided by the default
constructor.
Java Parameterized Constructor
A constructor which has a specific number of parameters is called a parameterized
constructor.
Why use the parameterized constructor?
The parameterized constructor is used to provide different values to distinct
objects. However, you can provide the same values also.
class Student{
int id;
String name;
Student(int i,String n){
id = i;
name = n;
}
void display(){System.out.println(id+" "+name);}
}
Class TestStudent{
public static void main(String args[]){
Student s1 = new Student (1, "Krishna");
Student s2 = new Student (2, "Narayani");
s1.display();
s2.display();
} }
Output
1 Krishna
2 Narayani
class Student{
int id,age;
String name;
Student(int i,String n){
id = i;
name = n;
}
Student(int i,String n,int a){
id = i;
name = n;
age=a;
}
void display(){
System.out.println(id+" "+name+" "+age);
} }
class TestStudent{
public static void main(String args[]){
Student s1 = new Student(11,"Sri");
Student s2 = new Student(22,"Nath",25);
s1.display();
s2.display();
} }
Output
11 Sri
12 Nath 25
The static variable can be used to refer to the common property of all objects
(which is not unique for each object), for example, the company name of employees, the
college name of students, etc.
The static variable gets memory only once in the class area at the time of class
loading.
class Countstatic{
int count=0;
static int cnt=0; //will get memory only once
Countstatic(){
count++;
cnt++;
System.out.println("Normal Counter\t"+count);
System.out.println("Static counter\t"+cnt);
}
public static void main(String args[]){
//creating objects
Countstatic c1=new Countstatic();
Countstatic c2=new Countstatic();
Countstatic c3=new Countstatic();
} } Output
Normal Counter 1 Static Counter 1
Normal Counter 1 Static Counter 2
Normal Counter 1 Static Counter 3
A static method belongs to the class rather than the object of a class.
A static method can be invoked without the need for creating an instance of a class.
A static method can access static data members and can change their value.
class demoStatic{
// Static method
static void disp(){
System.out.println("disp block in demo");
}}
class testStatic{
public static void main(String arg[]){
demoStatic.disp(); //calling static method
System.out.println("Main Method");
}
//static block 1
static{
System.out.println("Static block1 in Test");
}
// static block 2
static{
System.out.println("Static block2 in Test ");
} }
Output
Static block1 in Test
Static block2 in Test
disp block in demo
Main Method
Note: If you are beginner to java, lookup only three usages of this keyword.
In the above example, parameters (formal arguments) and instance variables are the
same. Hence creating an ambiguous state, and are not able to assign the values to instance
variables. To avoid such an ambiguous state this keyword is used as shown below.
class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
void display(){
System.out.println(rollno+"\t "+name+"\t "+fee);
} }
class TestThis{
public static void main(String args[]){
Student s1=new Student(2,"Aiyan ",5000);
Student s2=new Student(3,"Padminin",6000);
s1.display();
s2.display();
}}
Output
2 Aiyan 5000
3 Padmini 6000
Example Program to represent how this keyword is used in invoking methods of a class
import java.util.*;
class ThisMethod {
String name;
void setName(String name) {
this.name = name; }
String getName() {
return name; }
void disp() {
System.out.println("Student Name is: "+this.getName());
}}
class ThisMethodTest{
public static void main(String args[]) {
ThisMethod obj = new ThisMethod();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the name of the student: ");
String name = sc.nextLine();
obj.setName(name);
obj.disp();
}} Output
Enter the name of the student – Adithya Barade
Student Name is – Adithya Barade
class A{
A() {
System.out.println("Default Constructor");
}
A(String name){
this();
System.out.println(name);
}}
class B{
public static void main(String arg[]){
A oba=new A("Hello");
A oba1=new A("HI");
}}
Output
Default Constructor
Hello
Default Constructor
Hi