Professional Documents
Culture Documents
Java Chapter 3
Java Chapter 3
Java Methods
Command Line Arguments
Constructors
this Keyword
super Keyword
static Keyword
final Keyword
finally
Java Methods
A java method is a set of statements that are used to perform some task.
Naming Convention –
Method name should start with lowercase letter.
If the method name contains multiple words, start it with a lowercase letter followed by an uppercase
letter such as getData().
Creating Method –
First you write return type of method then method name.
void show() {
// Statements;
}
Method Calling – There are two ways in which method is called in java:
With return a value
With no return value
Java Methods
With Return a Value With No Return a Value
class Test { class Test {
public static void main(String args []) { public static void main(String args []) {
int c = show(); Test t = new Test();
System.out.println(c); t.show();
} }
int show() { void show() {
int z = 10 + 20; int z = 10 + 20;
return z; System.out.println(z);
} }
} }
Output - 30 Output - 30
Java Methods
Passing parameters by value:
If a class has more than one method with same name but different parameters is known as Method
Overloading.
Passing Parameters by Value means calling a method with a parameter.
There are ways to overload the method in java
Execution Steps –
javac Test.java
java Test 10 20
Output – sum = 30
Constructors
I java, Constructor is a special type of method. It is called at the time of object creation. Memory of the
object is allocated at the time calling constructor.
Every class must have a constructor. If there is no constructor in class then java compiler automatically
creates a default constructor.
s1.display();
s2.display();
}
}
Output – 0 null
0 null
Constructors
In java, there are three types of constructor:
Parameterized Constructor
Non – Parametrized Constructor
Default Constructor
A constructor has a specific parameter is called Parameterized Constructor. A constructor with no parameter is
called Non-Parameterized Constructor.
Constructors
Parameterized Constructor Non-Parameterized Constructor
A constructor has a specific parameter is called Parameterized Constructor. A constructor with no parameter is
called Non-Parameterized Constructor.
Default Constructor
Constructor generated by compiler if there is no constructor in program, that constructor is known as Default
Constructor.
Constructors
Constructor Overloading
Constructor Overloading means that a class having more than one constructor with different parameter.
void show(){
Constructor Overloading
System.out.println("Id = "+id+"\t"+"Name = "+name);
class Demo{
}
int id, salary;
void show1(){
String name;
System.out.println("Id = "+id+"\t"+"Name = "+name+"\tSalary = "+salary);
Demo(int i, String n){
}
id = i;
public static void main(String args[]){
name = n;
Demo t=new Demo(101, "Manish");
}
Demo t1=new Demo(102, "Manish", 2000);
Demo(int i, String n, int sal){
t.show(); t1.show1();
id = i;
}}
name = n; salary = sal;
Output – Id = 101 Name = Manish
}
Id = 102 Name = Manish Salary = 2000
Constructors
Constructor Vs. Method
A constructor must not have a return A method must have a return type.
type.
The constructor is invoked implicitly. The method is invoked explicitly.
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 The method name may or may not be same
as the class name. as the class name.
this Keywords
In java, this is a reference variable that refers to the current class object.
this keyword is used to differentiate local and instance variable when both the variable name is same.
Static Block - In java static block is used to initialize static data member and it is executed before the main
method at the time of class loading. class Demo{
static {
System.out.println("Static Block");
}
public static void main(String args[]) { Output - Static Block
System.out.println("Main Method"); Main Method
}
}
static keyword
Static Variable
A variable declare with static keyword is called Static Variable.
After declaration static variable, a single copy of the variable is created and divided among all objects at the
class level.
Static variable can be created at class-level only and it gets memory only once in the class area at the time of
class loading.
Static variable can’t re-initialized.
class Demo{ void show() {
int e_id; System.out.println("Emp_Id = "+e_id+"\t Emp_Name = "+name+"\t
String name; Company ="+company);
static String company = "PSIT"; }
Demo (int i, String n) { public static void main(String args[]) {
e_id = i; Demo d = new Demo(101, "Manish");
name = n; Demo d1 = new Demo(102, "Vishal");
} d.show();
Output - Emp_Id = 101 Emp_Name = Manish Company =PSIT
d1.show();
Emp_Id = 102 Emp_Name = Vishal Company =PSIT
}}
static keyword
Static Variable
static keyword
Static Method
A nested class can be static. Nested static class doesn’t need a reference of outer class.
A static class cannot access non-static members of the outer class.
You compile and run your nested class as usual that with outer class name.
Variable - If you declare a variable with final keyword then it must be initialized.
Final Variable Final Variable (Can’t change value)
class Demo { class Demo {
final int x = 10; final int x = 10;
void show() { void show() {
System.out.println(x); x = 20;
} System.out.println(x);
public static void main(String args[]) { }
Demo d = new Demo(); public static void main(String args[]) {
d.show(); Demo d = new Demo();
}} d.show();
Output - 10 }}
Output - error: cannot assign a value to final variable x
final keyword
Method - If you declare a method as final it means that you cannot override (Discuss in Inheritance) this method.
class Test {
final void show() {
System.out.println("Parent Class Method");
}
}
class Demo extends Test{
void show(){
System.out.println("Cannot override due to final method.");}
public static void main(String args[]) {
Demo d = new Demo();
d.show();
}
}
finally {
// Statements;
}