Chapter 2.1
Chapter 2.1
Derived Syntactical
Constructs in Java
Mr. R. M. Patil
SY CO, JPR
2023-2024
Methods in Java
A method is a block of code which only runs when it is called.
You can pass data, known as parameters, into a method.
Methods are used to perform certain actions, and they are also
known as functions.
Why use methods?
To reuse code: define the code once, and use it many times.
public class test
{
void myMethod()
{
// code to be executed
}
}
Methods in Java
• void means that this method does not have a return value.
Methods in Java - Call a Method (Without Object)
public class test
{
static void myMethod()
{
System.out.println("I just got executed!");
}
public static void main(String[] args)
{
myMethod();
myMethod();
myMethod(); OUTPUT:
} I just got executed!
} I just got executed!
I just got executed!
Methods in Java - Call a Method (With Object)
public class test
{
void myMethod()
{
System.out.println("I just got executed!");
}
public static void main(String[] args)
{
test t1 = new test();
t1.myMethod();
t1.myMethod(); OUTPUT:
t1.myMethod(); I just got executed!
} I just got executed!
} I just got executed!
2.1. Nesting of Methods in Java
When a method in java calls another method in the
same class, it is called Nesting of methods.
method2()
{
class Test // statements
{ method1();
method1() }
{ method3()
// statements {
} // statements
method2();
}
}
2.1. Nesting of Methods in Java - Example
class Test void display()
{ {
int m,n; int lagre=largest();
Test(int x, int y) System.out.println(“Largest=”+large);
{ }
m=x; }
n=y; class Demo
} {
int largest() public static void main(String args[])
{ {
if(m>=n) Test t=new Test(50,40);
return(m); t.display();
else }
return(n); }
}
2.1. 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.
Output : 10 20
2.1. Constructors in Java – 2. Parameterized Constructor
class Student {
{ public static void main(String args[])
int id; {
String name; Student s1=new Student(01,”XYZ”);
Student s2=new Student(02, “abc”);
Student (int a, String nm) s1.display();
{ s2.display();
id = a; }
name = nm; }
}
void display()
{
System.out.println(id+" "+name);
}
}
A constructor must not have a return A method must have a return type.
type.
The Java compiler provides a default The method is not provided by the
constructor if you don't have any compiler in any case.
constructor in a class.
The constructor name must be same as The method name may or may not be
the class name. same as the class name.
Homework
1. What is Constructor? Or Define Constructor.
W14, W15, S16 – 02 M
2. Enlist types of Constructor. Explain any two with Example.
S18 - 04 M
3. Create a class with 3 data members. Define a constructor to set
values of first two data members. Define a method to calculate
addition of numbers and print the result. – 04 M
4. Demonstrate the use of parameterized constructor with suitable
example.
W14, W15, S16 – 02 M
5. How to overload constructor? Explain with suitable example. 04M
2.1. Command Line Arguments in Java
class CommandLineExample
{
public static void main(String args[])
{
System.out.println("Your first argument is: "+args[0]);
System.out.println("Your second argument is: "+args[1]);
}
}
class test
{
test()
{
System.out.println("this method called..");
}
test(int x) Output:
{ this method called..
this(); x=10
System.out.println("x="+x);
}
public static void main(String args[])
{
test t = new test(10);
}
}
2.1. varargs : ‘Variable length arguments’
Let’s suppose you are creating a Java method.
However, you are not sure how many arguments your
method is going to accept.
Rules:
Examples of varargs that fails to compile:
There can be only
void method(String... a, int... b){ } one variable
argument in the
//Compile time error
method.
void method(int... a, String b){ }
Variable
//Compile time error argument (varargs)
must be the last
argument.
Example of Varargs that is the last argument in the method:
class VarargsExample3
{
static void display(int num, String... values) Output:
{
number is 500
System.out.println("number is "+num); hello
for(String s:values) number is 1000
{ my
System.out.println(s); name
is
} varargs
}
public static void main(String args[])
{
display(500,"hello"); //one argument
display(1000,"my","name","is","varargs"); //four arguments
}
}
2.1. Garbage collection in java
In java, garbage means unreferenced objects.
3) By anonymous object:
new Employee();
2.1. Garbage collection in java
We can also request JVM to run garbage collector..
Method Description
public final void wait(long timeout)throws causes the current thread to wait for the
InterruptedException specified milliseconds, until another thread
notifies (invokes notify() or notifyAll()
method).
public final void wait(long timeout,int causes the current thread to wait for the
nanos)throws InterruptedException specified milliseconds and nanoseconds,
until another thread notifies (invokes
notify() or notifyAll() method).
public final void wait()throws causes the current thread to wait, until
InterruptedException another thread notifies (invokes notify() or
notifyAll() method).
protected void finalize()throws Throwable is invoked by the garbage collector before
object is being garbage collected.
Homework
1. What is ‘this’? Give Example. 02 M
2. Explain ‘varargs’ with suitable example. 04 M
3. What is garbage collection in Java? Explain finalize
method in Java.
S17 - 04 M
4. Explain the importance of object class and explain any
three methods of this class. – 04 M