Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .
Ltd
Methods and constructors in
java
Shanu k k
shanukk89@gmail.com
www.facebook.com/shanunn
i
twitter.com/shanu
in.linkedin.com/in/shanu k k
9656153432
Typing Speed
Week

Target Achieved

1

25

25

2

27

30

3

35

29
Jobs Applied
#
1
2
3

Company

Designation

Applied Date

Current Status
Method
oA Java method is a collection of
statements that are grouped together to
perform an operation
o A class without method has no life,the
object created by such class cannot respond
to any message
o Methods are declared inside the body of
the class but immediately after the
declaration of the instance variable
Method declaration
Type methodname(parameter_list)
{
Method_body;
}
Eg:
Class rectangle
{
int length, width;
Void getData(int x,int y)
{
Length=x;
Width=y;
}
}
class sum {
void sum(int a,int b)
{
System.out.println( (a+b));
}
void sum(int a,int b,int c)
{
System.out.println( (a+b+c));
}
public static void main(String args[])
{
sum obj=new sum();
obj.sum(10,5);
obj.sum(10,5,10);
Constructor:
o Object we created must be given to instial
values
o We have done earlier using two
approaches
1
2

Dot operator:access the instance variable and assign value
them indiviidualy
getData method:intialise each object individually
it would be simpler to initialise an object when
it’s first created _special type of method is
called construcor
• Constructor have the same name as the class
itself
• They do not specify any return type not even
void
•

class Constructor Demo{
Constructor Demo(){
System.out.println("Inside no argument constructor");
}
ConstructorDemo(String name){
System.out.println("Inside one argument constructor in Java with
name: " + name);
}
public static void main(String args[]) {
Constructor Demo d = new ConstructorDemo(); //calling no
argument constructor in java
Constructor Demo e = new ConstructorDemo("Testing"); //calling
one argument constructor in java
}

}
• Output:
Inside no argument constructor
Inside one argument constructor in Java with
name: Testing
•
If this presentation helped you, please visit our
page facebook.com/baabtra and like it.

Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550

Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com

NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550

Methods and constructors in java

  • 2.
    Disclaimer: This presentationis prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  • 3.
    Methods and constructorsin java Shanu k k shanukk89@gmail.com www.facebook.com/shanunn i twitter.com/shanu in.linkedin.com/in/shanu k k 9656153432
  • 4.
  • 5.
  • 6.
    Method oA Java methodis a collection of statements that are grouped together to perform an operation o A class without method has no life,the object created by such class cannot respond to any message o Methods are declared inside the body of the class but immediately after the declaration of the instance variable
  • 7.
  • 8.
    Eg: Class rectangle { int length,width; Void getData(int x,int y) { Length=x; Width=y; } }
  • 9.
    class sum { voidsum(int a,int b) { System.out.println( (a+b)); } void sum(int a,int b,int c) { System.out.println( (a+b+c)); } public static void main(String args[]) { sum obj=new sum(); obj.sum(10,5); obj.sum(10,5,10);
  • 10.
    Constructor: o Object wecreated must be given to instial values o We have done earlier using two approaches 1 2 Dot operator:access the instance variable and assign value them indiviidualy getData method:intialise each object individually
  • 11.
    it would besimpler to initialise an object when it’s first created _special type of method is called construcor • Constructor have the same name as the class itself • They do not specify any return type not even void
  • 12.
    • class Constructor Demo{ ConstructorDemo(){ System.out.println("Inside no argument constructor"); } ConstructorDemo(String name){ System.out.println("Inside one argument constructor in Java with name: " + name); } public static void main(String args[]) { Constructor Demo d = new ConstructorDemo(); //calling no argument constructor in java Constructor Demo e = new ConstructorDemo("Testing"); //calling one argument constructor in java } }
  • 13.
    • Output: Inside noargument constructor Inside one argument constructor in Java with name: Testing •
  • 14.
    If this presentationhelped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  • 15.
    Contact Us Emarald Mall(Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550