JAVA CLASS AND
OBJECTS
By: Dr. Sangeeta Sen
Declare and Allocate
Example:
int myArray [ ] [ ];
myArray = new int [3] [4];
OR
int myArray [ ] [ ] = new int [3] [4];
Initializing a 2D array :
An example
int myArray [2] [3] = {1, 2, 3, 4, 5, 6};
OR
int myArray [ ] [ ] = { {1, 2, 3}, {4, 5, 6} };
Creating and initializing 2D array
public class Boy{
String name;
String address;
int age;
public static void main(String[] args) {
Boy obj = new Boy();
obj.name = “Rohan";
obj.address = “Durgapur";
obj.age = 23;
System.out.println("Name of the boy is:
"+obj.name);
System.out.println("Address of the boy is:
"+obj.address);
System.out.println("Age of the boy is: "+obj.age);
}}
Class and its object creation in Java
Syntax for creating an object in Java is as shown below:
ClassName object-reference = new ClassName();
Class and its object creation in Java
Objects are created dynamically in the heap
area inside RAM (Random Access Memory).
Class and its object creation in Java
public class Boy{
String name;
String address;
int age;
void printData(String x, String y, int a){
name = x;
address = y;
age = a ;
System.out.println("Name of the boy is: "+name);
System.out.println("Address of the boy is: "+address);
System.out.println("Age of the boy is: "+age);}
public static void main(String[] args) {
Boy obj = new Boy();
obj.printData("Riya", "Durgapur", 23); }}
public class StringTest {
public static void main(String[] args) {
String[] authors = {"Herbert Schildt","Ivor Horton","Josuah Boch","Debasis Roy",
"Gary Cornell"};
System.out.println("The best author is " + authors[0]);
System.out.print("n" + authors[4]);
System.out.println();
System.out.println(authors[3]);
System.out.println(authors[5]);
}
}
Guess the output
Write programs
Following is a small program in C. Write an
equivalent program using Java language.
#include……
int main() {
int x = 10, y = 3;
int a, b;
a = x + y;
b = x - y;
printf(“x + y = %dn”, a);
printf(“x - y = %dn”, b);
getch();
return 0;
}
Write an equivalent program of the following program using Java
language.
#include …..
void main() {
int i = 0;
printf(“Enter a value of i : “);
scanf(“%d”, &i);
printf(“n n i = %d, i++ = %d, ++i = %dn”, i, i++, i++);
return; }
Scanner scan = new Scanner(System.in);
System.out.print("Enter a value of i : ");
i = scan.nextInt();
Class and its object creation in Java
1. Program to Print a Welcome Message (class name: WelcomeMessage, object
name: wm, method name: printMessage())
2. Program to Find Area of a Rectangle (class name: Rectangle, object name: c,
method name: calArea())
3. Program to Multiply Two Numbers (class name: Product, object name: p, method
name: multiplyNo())
4. Program to Display Bank Account Details of two account holder (class name:
Bank, object name: b1, b2, method name: printDetails(name, account details))
class Circle {
double r;
double circumference() {
return 2 * 3.14159 * r; }
double area() {
return (22 / 7) * r * r; }}
class CircleTest {
public static void main(String args[]) {
Circle c1 = new Circle();
c1.r = 12.36;
System.out.println("Area = " + c1.area() + "t" + "Circumference= " + c1.circumference()); }}
Class and its object creation in Java

Contains concept of Java class and objects

  • 1.
  • 2.
    Declare and Allocate Example: intmyArray [ ] [ ]; myArray = new int [3] [4]; OR int myArray [ ] [ ] = new int [3] [4]; Initializing a 2D array : An example int myArray [2] [3] = {1, 2, 3, 4, 5, 6}; OR int myArray [ ] [ ] = { {1, 2, 3}, {4, 5, 6} }; Creating and initializing 2D array
  • 3.
    public class Boy{ Stringname; String address; int age; public static void main(String[] args) { Boy obj = new Boy(); obj.name = “Rohan"; obj.address = “Durgapur"; obj.age = 23; System.out.println("Name of the boy is: "+obj.name); System.out.println("Address of the boy is: "+obj.address); System.out.println("Age of the boy is: "+obj.age); }} Class and its object creation in Java Syntax for creating an object in Java is as shown below: ClassName object-reference = new ClassName();
  • 4.
    Class and itsobject creation in Java Objects are created dynamically in the heap area inside RAM (Random Access Memory).
  • 5.
    Class and itsobject creation in Java public class Boy{ String name; String address; int age; void printData(String x, String y, int a){ name = x; address = y; age = a ; System.out.println("Name of the boy is: "+name); System.out.println("Address of the boy is: "+address); System.out.println("Age of the boy is: "+age);} public static void main(String[] args) { Boy obj = new Boy(); obj.printData("Riya", "Durgapur", 23); }}
  • 6.
    public class StringTest{ public static void main(String[] args) { String[] authors = {"Herbert Schildt","Ivor Horton","Josuah Boch","Debasis Roy", "Gary Cornell"}; System.out.println("The best author is " + authors[0]); System.out.print("n" + authors[4]); System.out.println(); System.out.println(authors[3]); System.out.println(authors[5]); } } Guess the output
  • 7.
    Write programs Following isa small program in C. Write an equivalent program using Java language. #include…… int main() { int x = 10, y = 3; int a, b; a = x + y; b = x - y; printf(“x + y = %dn”, a); printf(“x - y = %dn”, b); getch(); return 0; } Write an equivalent program of the following program using Java language. #include ….. void main() { int i = 0; printf(“Enter a value of i : “); scanf(“%d”, &i); printf(“n n i = %d, i++ = %d, ++i = %dn”, i, i++, i++); return; } Scanner scan = new Scanner(System.in); System.out.print("Enter a value of i : "); i = scan.nextInt();
  • 8.
    Class and itsobject creation in Java 1. Program to Print a Welcome Message (class name: WelcomeMessage, object name: wm, method name: printMessage()) 2. Program to Find Area of a Rectangle (class name: Rectangle, object name: c, method name: calArea()) 3. Program to Multiply Two Numbers (class name: Product, object name: p, method name: multiplyNo()) 4. Program to Display Bank Account Details of two account holder (class name: Bank, object name: b1, b2, method name: printDetails(name, account details))
  • 9.
    class Circle { doubler; double circumference() { return 2 * 3.14159 * r; } double area() { return (22 / 7) * r * r; }} class CircleTest { public static void main(String args[]) { Circle c1 = new Circle(); c1.r = 12.36; System.out.println("Area = " + c1.area() + "t" + "Circumference= " + c1.circumference()); }} Class and its object creation in Java