Java Lec 4
Java Lec 4
1. To Make the Code Reusable: If you need to do the same thing many
times, methods allow you to write code once and reuse it multiple times.
Method Type
1. Instance methods are used to access the instance variables but can also
access class variables.
2. Class methods can access class variables but cannot access the instance
variables unless and until they use an object for that.
Method Declaration:
Syntax:
Example:
int methodOne (int x, String y)
Modifier Description
public, protected, Defines the scope—which class can invoke which method.
default or private
static The method can be invoked without creating an instance of
the class.
abstract The class must be extended and the abstract method must be
overridden in the subclass.
final The method cannot be overridden in a subclass.
2. Return Type: Specifies the data type of the value returned by the
method.
3. Method Name: The method name must be a valid Java identifier.
4. ParameterList: Defines the arguments the method accepts.
5. Curly Braces: The method body is contained in a set of curly braces.
Instance Method Invocation:
Example:
package Simple_Package;
class Example {
Example:
package Simple_Package;
public class OverloadDemo {
void addition(int a, int b)
{
int sum=a+b;
System.out.println("Sum = " + sum);
}
void addition(int a, int b,int c)
{
int sum=a+b+c;
System.out.println("Sum = " + sum);
}
void addition(int a, int b,intc,int d)
{
int sum=a+b+c+d;
System.out.println("Sum = " + sum);
}
}
this Keyword
‘this’ is a reference variable that is used to refer the current object of the
class.
The most common use of this keyword is to eliminate the confusion
between instance variables and local variables with the same name.
It can be used to access instance variables and methods of the current
object.
this can also be used to:
o refer to current class instance variables
o Invoke current class constructor
o Invoke current class method
o Return the current class object
o Pass an argument in the method call
o Pass an argument in the constructor call
Example:
package Simple_Package;
public class Rectangle {
double length;
double width;
rect.displayArea();
}}
Constructors
Example:
package Simple_Package;
public class Rectangle {
double length;
double width;
public Rectangle() {
this.length = 1.0;
this.width = 1.0;
}
public void displayArea() {
double area = length * width;
System.out.println("Length: " + length);
System.out.println("Width: " + width);
System.out.println("Area: " + area);
}
Parameterized Constructors:
Constructor that accepts parameters is known as parameterized
constructor.
It is used to initialize the instance variables with specific values.
Example:
package Simple_Package;
public class Rectangle {
double length;
double width;
Constructor Overloading:
Example:
package Simple_Package;
Arrays:
One-dimensional Arrays:
One-dimensional array is a linear collection of elements of the same
data type.
Elements of an array can be accessed using their index. The index
represents the position of an element in the array.
The indexation will start from 0 and will go up to n –1, i.e., the first
value of the array will have an index of 0 and the last value will have an
index of n –1, where n is the number of elements in the array.
Suppose, five marks to be assigned to each array element are 60, 58, 50, 78,
and 89. It will be done as follows:
Creation of Array
Creating an array involve three steps:
1) Declaring an array
2) Creating memory locations
3) Initializing/assigning values to an array
1) Declaring an Array:
Declaring an array can be two ways:
o type arrayname[];
o type[] arrayname;
Example:
int marks[];
int[] marks;
Example:
int[] marks = new int[5];
Arrayname[index] = value;
Example:
Example:
Two-dimensionalArrays:
For example:
The for-each loop also called the enhanced for loop was introduced in
Java 5 to provide a simpler way to iterate through all the elements of an
array or a collection.
A for-each loop directly accesses elements without using index.
Syntax:
Parameters:
type: The data type of the elements in the array or collection.
var: The variable that holds the current element during each iteration.
array: The array or collection being iterated over.
Example:
package First_Package;
public class First_Program {
void printArray(int[] arr) {
for (int i = 0; i<arr.length; i++)
{
System.out.println(arr[i]);
}
}
public static void main(String[] args) {
First_Program fp = new First_Program();
int[] numbers = {10, 20, 30, 40, 50};
fp.printArray(numbers);
}
}
Example:
package First_Package;
public class First_Program {
int[] createArray()
{
int[] arr = {10, 20, 30, 40, 500};
return arr; // Return the array
}
}
}
Command-line Arguments
Command-line argument in Java is a way to pass data to a program
when it is executed.
These arguments are supplied at runtime and are accessible in the
program through the String[] args parameter of the main method.
Command-line arguments are stored in the args array.
Each argument is a string, and the first argument is at args[0], the
second at args[1], and so on.
Example:
package First_Package;
public class First_Program {
int x = args.length;
for (int i = 0; i< x; i++)
System.out.println(args[i]);
}
}
Nested Classes.
3. Local Classes:
A local class is a class defined inside a method or a block. It has access to the
local variables of the method or block.
public class OuterClass {
public void myMethod()
{
int x = 10;
class LocalClass
{
public void printX()
{
System.out.println("Value of x: " + x);
}
}
LocalClass local = new LocalClass();
local.printX(); // Output: Value of x: 10
}