Introduction to Classes Objects and Methods (1)
Introduction to Classes Objects and Methods (1)
Department of CSE
Syntax of Class
Department of CSE
Assigning Object Reference Variables
Department of CSE
Methods
type method_name(parameter-list) {
// body of method
Department of CSE
class BoxDemo3 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
// This program includes a method inside the box
// assign values to mybox1's instance variables
class.
mybox1.width = 10;
class Box {
mybox1.height = 20;
double width;
mybox1.depth = 15;
double height;
/* assign different values to mybox2's
double depth;
instance variables */
// display volume of a box
mybox2.width = 3;
void volume() {
mybox2.height = 6;
System.out.print("Volume is ");
mybox2.depth = 9;
System.out.println(width * height * depth);
// display volume of first box
}
mybox1.volume();
}
// display volume of second box
mybox2.volume();
}
Department of CSE }
class BoxDemo3 {
Returning a Value public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// This program includes a method inside the // assign values to mybox1's instance variables
box class. mybox1.width = 10;
class Box { mybox1.height = 20;
double width; mybox1.depth = 15;
/* assign different values to mybox2's
double height;
instance variables */
double depth; mybox2.width = 3;
// display volume of a box mybox2.height = 6;
double volume() { mybox2.depth = 9;
return width * height * depth; // get volume of first box
vol = mybox1.volume();
}
System.out.println("Volume is " + vol);
} // get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
Department of CSE
}
Method without parameters
Method with
parameters
int square()
{ int square(int i)
return 10 * 10; {
} return i * i;
}
int x;
x = square(5); // x equals 25
x = square(9); // x equals 81
Department of CSE
// This program uses a parameterized method. class BoxDemo5 {
class Box { public static void main(String args[]) {
double width; Box mybox1 = new Box();
double height; Box mybox2 = new Box();
double depth; double vol;
// compute and return volume // initialize each box
double volume() { mybox1.setDim(10, 20, 15);
return width * height * depth; mybox2.setDim(3, 6, 9);
} // get volume of first box
// sets dimensions of box vol = mybox1.volume();
void setDim(double w, double h, double d) { System.out.println("Volume is " + vol);
width = w; // get volume of second box
height = h; vol = mybox2.volume();
depth = d; System.out.println("Volume is " + vol);
} }
} }
Department of CSE
Constructors
Department of CSE
Types of Constructors
Department of CSE
Types of Constructors
• The constructors are the constructors having a specific number of arguments to be passed.
Department of CSE
Default Constructor
class Box {
double width; class BoxDemo6 {
double height; public static void main(String args[]) {
double depth; Box mybox1 = new Box();
// This is the constructor for Box. Box mybox2 = new Box();
Box() { double vol;
System.out.println("Constructing Box"); // get volume of first box
width = 10; vol = mybox1.volume();
height = 10; System.out.println("Volume is " + vol);
depth = 10; // get volume of second box
} vol = mybox2.volume();
// compute and return volume System.out.println("Volume is " + vol);
double volume() { }
return width * height * depth; }
}
}
Department of CSE
Parameterized Constructors
class Box {
double width; class BoxDemo7 {
double height; public static void main(String args[]) {
double depth; Box mybox1 = new Box(10, 20, 15);
// This is the constructor for Box. Box mybox2 = new Box(3, 6, 9);
Box(double w, double h, double d) { double vol;
width = w; // get volume of first box
height = h; vol = mybox1.volume();
depth = d; System.out.println("Volume is " + vol);
} // get volume of second box
// compute and return volume vol = mybox2.volume();
double volume() { System.out.println("Volume is " + vol);
return width * height * depth; }
} }
}
Department of CSE
The this Keyword
this can be used inside any method to refer to the current object.
The this keyword is an essential concept in Java for handling ambiguity and
managing class objects efficiently.
Department of CSE
The this Keyword (Refers to the current class instance.)
class Sample {
int n1; class Sample {
int n2; int n1;
Sample(int n1, int n2) { int n2;
n1=n1; Ambiguity
Sample(int n1, int n2) {
n2=n2; Situation this.n1 = n1
}
this.n2 = n2
void display() {
System.out.println(n1+” ”+n2); }
} void display() {
} System.out.println(n1+” ”+n2);
}
}
Department of CSE
The this Keyword (Differentiates between instance variables
and method parameters.)
Department of CSE
The this Keyword
(Invokes current class methods (this.methodName())
class Example {
void method1() {
System.out.println("Method1 called");
}
Department of CSE
The this Keyword
(Calls current class constructors (this()).
this() can be used to call another constructor in the same class.
It must be the first statement in the constructor.
class Example {
Example() {
this(10); // Calls the parameterized constructor
System.out.println("Default constructor"); public class Main {
} public static void main(String[] args)
Example(int num) { {
System.out.println("Parameterized constructor: " + Example obj = new Example();
num); }
} }
}
Department of CSE
The this Keyword
(Passes the current object as a method/constructor argument)
You can pass the current object (this) as an argument to a method,
enabling the method to operate on that instance.
class Example {
void display(Example obj) { public class Main {
// Method accepts an Example object public static void main(String[] args)
System.out.println("Method called with current {
object reference"); Example obj = new Example();
} obj.callDisplay();
// Output: Method called with current
void callDisplay() { object reference
display(this); // Passing the current object }
} }
}
Department of CSE
The this Keyword
(Returns the current class instance)
In Java, the this keyword can be used to return the current class instance from a
method.
This is particularly useful for method chaining, where multiple methods are called on
the same object in a single statement.
Department of CSE
The this Keyword
(Returns the current class instance.)
class Example {
int num;
Department of CSE
The this Keyword
(Refers to an outer class instance in nested classes)
Department of CSE
The this Keyword
(Refers to an outer class instance in nested classes)
class Outer {
int x = 10;
The wrapper classes in Java are used to convert primitive types into corresponding objects and
objects into primitive types.
Department of CSE
Parsing
The parse() method receives some string as input, "extracts" the necessary information from it.
Example:
Integer.parseInt() to convert string into int
Double.parseDouble() to convert string into double
Department of CSE
Java Command Line Arguments
The java command-line argument is an argument i.e. passed at the time of running the java program.
class CmdDemo{
public static void main(String args[]){
int count = args.length;
for(int i=0;i<count;i++)
System.out.println(args[i]);
}
}
Department of CSE
Java Scanner Class
The Scanner class of the java.util package is used to read input data from different sources like input
streams, users, files, etc.
Department of CSE
Reading from System.in
InputStreamReader ir = new InputStreamReader(System.in)
BufferedReader br = new BufferedReader(ir);
Department of CSE
Methods in BufferedReader
read()
readLine()
Department of CSE
import java.io.*;
class B
{
public static void main(String args[]) throws IOException
{
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
String str = br.readLine();
System.out.println("String is "+str);
}
}
Department of CSE
Thank You
All the Best
Department of CSE