Professional Documents
Culture Documents
5-Arr - Methods, Class-Ob, Use of Static KW (Unit-1)
5-Arr - Methods, Class-Ob, Use of Static KW (Unit-1)
Arrays
Methods
Constructors
79 87 94 82 67 98 87 81 74 91
scores
An array of size N is indexed from zero to N-1 This array holds 10 values that are indexed from 0 to 9
Value in an array is referenced by the array name followed by the index in brackets.
For example, scores[2]: refers to the value 94 (the 3rd value in the array)
Syntax to declare:
1.dataType[][] arrayRefVar; (or) Instantiation:
2.dataType arrayRefVar[][]; (or)
3.dataType []arrayRefVar[]; int[][] arr=new int[3][3]; //3 row,3 column
//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println(); } }}
CLASS AND OBJECT
Classes and objects are the two main aspects of OOP
OOPs (Object-Oriented Programming System)
Object means a real-world entity such as a pen, chair, table, computer, watch, etc.
OOP is a methodology or paradigm to design a program using classes and objects. It simplifies s/w development and
maintenance by providing some concepts:
•Object
•Class
•Inheritance
•Polymorphism
•Abstraction
•Encapsulation
a) object
• An entity that has state and behavior is known as an object
e.g., chair, bike, marker, pen, table, car, etc.
A class is a template or blueprint from which objects are created. So, an object is the
instance(result) of a class.
b) class
A class is a group of objects which have common properties. It is a template or blueprint from which objects are
created.
It is a logical entity. It can't be physical. Syntax to declare a class:
class <class_name>{
A class in Java can contain:
field;
Fields method;
Methods }
Constructors
Blocks
Nested class and interface
Create an Object
An object is created from a class.
To create an object, specify the class name, followed by the object name, and use keyword new:
class Student.
A simple class example {
Suppose, Student is a class and student's name, roll number, age are its String name;
fields and info() is a method. Then class will look like below. int rollno;
int age;
void info(){
// some code
}
}
Ex: Create Student class
Student class 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.
(1) Predefined Method: Methods that is already defined in the Java class libraries.
•It is also known as the standard library method or built-in method.
•We can directly use these methods just by calling them in the program at any point.
•Some pre-defined methods are length(), equals(), compareTo(), sqrt(), etc.
Output:
Maximum number is: 9
2. User-defined Method
Method written by the user or programmer. These methods are modified according to the requirement.
Ex: Create and call user defined method(findEvenOdd) that checks the number is even or odd:
import java.util.Scanner;
public class EvenOdd {
}
Call by Value and Call by Reference in Java
1)call-by-value :
In this approach copy of an argument value is pass to a method.
Changes made to the argument value inside the method will have no
effect on the arguments.
2) call-by-reference :
Java does not support call by reference directly. However, we can
achieve similar behavior by passing objects as arguments since object
references are passed by value.
original value is not changed, because a copy of the argument is passed to the method, and
modifications to the parameter inside the method do not affect the original argument.)
class Student{
Example: int rollno; String name; static String college =“KIET";
Student(int r,String n)
{ rollno = r; name = n;
}
void display (){
System.out.println(rollno+" "+name+" "+college);}
public static void main(String args[])
{ Student s1 = new Student(111,"KIran");
Student s2 = new Student(222,"Aryan");
s1.display();
s2.display(); } }
Static method
• It is applying a static keyword with any method.
• Belongs to the class rather than object of a class.
• Can be invoked without the need for creating an instance of a class.
• Can access static data member and can change the value of it.
Example: Changing the common property of all objects(static field)
class Student { int rollno; String name;
static String college = “KIET";
static void change()
{ college = “KRISHNA"; }
Student(int r, String n)
{ rollno = r; name = n; }
void display ()
{System.out.println(rollno+" "+name+" "+college);}
public static void main(String args[])
{ Student.change();
Student s1 = new Student (111,"Kiran"); Student s2 = new Student (222,"Aryan");
Student s3 = new Student (333,“Avi");
s1.display(); s2.display(); s3.display(); } }
Static block