top of page
Search
Writer's pictureAbhinaw Tripathi

Methods in Java


Methods in Java

A method represents a group of statements that performs a task.Methods has two parts.

  1. Method Header or Method Prototype

  2. Method Body

Method Header:

it contains method name,method parameters and method return data type.Method Prototype written in forms:

return datatype methodName(parameter 1,parameter 2);

Method Body:

Below the method header,we should write the method body .Method body consists of a group of statement which can perform the tasks.

{

statements;

}

Understanding Methods

Lets take an example:

/**

*

*/

package com.myobjectrelation;

/**

* @author Abhinaw.Tripathi

*

*/

class Sample

{

private double num1,num2;

public Sample(double x,double y)

{

this.num1=x;

this.num2=y;

}

void sum()

{

double res=num1+num2;

System.out.println("Sum is " + res);

}

}

public class MethodExample

{

public static void main(String[] args)

{

Sample s=new Sample(10, 22.50);

s.sum();

}

}

Result:

Sum is 32.5

What is instance methods?

Ans: Instance methods are the methods which act on the instance variables of the class.To call the instance methods,we should use the form : objectname.methodName() .

Sample Program:

/**

*

*/

package com.myobjectrelation;

/**

* @author Abhinaw.Tripathi

*

*/

class Sample

{

private double num1,num2;

public Sample(double x,double y)

{

this.num1=x;

this.num2=y;

}

void sum()

{

double res=num1+num2;

System.out.println("Sum is " + res);

}

}

public class MethodExample

{

public static void main(String[] args)

{

Sample s=new Sample(10, 22.50);

s.sum();

}

}

Result:

Sum is 32.5

What is static method?

Ans: Static method are the methods which do not act upon the instance variables of a class.Static methods are declared as 'Static' .

So to call the static methods ,we need not to create an object.we can call a static method.

Sample:

/**

*

*/

package com.myobjectrelation;

/**

* @author Abhinaw.Tripathi

*

*/

class Sample

{

static double sum(double num1,double num2)

{

double res=num1+num2;

return res;

}

}

public class StaticSample

{

public static void main(String[] args)

{

double x=Sample.sum(10, 20);

System.out.println("Sum will be "+ x);

}

}

Result:

Sum will be 30.0

Static Methods:

A static method is a method that does not act upon instance variable of a class.The reason why static methods can not act on instance variables is that the JVM first executes the static methods and then only it creates the objects .Since the objects are not available at the time of calling the static methods,instance variables are also not available.

What is the difference between the instance variables and class variables(Static variables)?.

Ans:

  • An instance variables is a variables whose separate copy is available to each object.A class variables is a variable whose single copy in memory is shared by all objects.

  • Instance variables are created in the objects on heap memory.Class variables are stored on method area.

Why instance variables are not available to static methods?

Ans: After executing static methods,JVM creates the objects.So the instance variables of the objects are not available to static methods.

So, the execution sequence of JVM is the process where JVM executes first of all any static blocks in the java program.then it executes static methods and then it creates any objects needed by the program.Finally,it executes the instance methods.

Static Block

A Static block is a block of statements declared as static .

static {

statements;

}

JVM executes a static block on highest priority basis.This means JVM first goes to static block even before it looks for the main() method in the program.This can be understood from the below program.

Program:

package com.myobjectrelation;

/**

* @author Abhinaw.Tripathi

*

*/

public class TestSample

{

static

{

System.out.println("Static block");

}

public static void main(String[] args)

{

System.out.println("Static method");

}

}

Result:

Static block

Static method

Is it possible to compile and run a java program without writing main() method?

Ans: Yes,it is possible by using static block in the java program.

The keyword 'this'

this is a keyword that refers to the object of the class where it is used.In other words,this refers to the object of the present class.Generally,we write instance variables,constructor and methods in a class.All these members are referenced by this.

When an object is created to a class,a default reference is also created internally to the object.This default reference is nothing but this .

Sample:

/**

*

*/

package com.myobjectrelation;

/**

* @author Abhinaw.Tripathi

*

*/

class TestSampleDemo

{

private int x;

public TestSampleDemo()

{

this(55);

this.access();

}

TestSampleDemo(int x)

{

this.x=x;

}

void access()

{

System.out.println("x=" + x);

}

}

public class ThisKeywordRef

{

public static void main(String[] args)

{

TestSampleDemo s=new TestSampleDemo();

s.access();

}

}

Result:

x=55

x=55

Instance Methods

Instance methods are methods which act upon the instance variables.To call the instance methods,objects is needed,since the instance variables are contained in the object.The speciality of instance method is that they can access not only the instance variables but also the static variables directly.

They are of two types.

  1. Accessor methods - this method simply access or read the instance variables.they do not modify the instance variables.

  2. Mutator methods - this method not only access the instance variables but also modify them.

Program:

/**

*

*/

package com.myobjectrelation;

/**

* @author Abhinaw.Tripathi

*

*/

class Person

{

private String name;

private int age;

public String getName()

{

return name;

}

public void setName(String name)

{

this.name = name;

}

public int getAge()

{

return age;

}

public void setAge(int age)

{

this.age = age;

}

}

public class AccessorMutatorApp

{

public static void main(String[] args)

{

Person p1=new Person();

p1.setAge(28);

p1.setName("Abhinaw");

System.out.println("Person name:" + p1.getName());

System.out.println("Person age: " +p1.getAge());

}

}

Result:

Person name:Abhinaw

Person age: 28

Passing Primitive Data types to Methods

Primitive data types or fundamental data types represent single entities or single values.for example,

char,byte,short,int,long,float,double and boolean etc. because they store single values.They are passed to methods by value.this means when we pass primitive data types to a method, a copy of those will be passed to methods.therefore,any changes made to them inside the method will not affect them outside the method.

  • take a temporary variable temp.

  • preserve a copy of num1 into temp.

  • now store num2 into num1 .

  • store the previous copy of num1 from temp into num1.

Passing Objects to Methods

We can also pass class objects to methods, and return objects from the methods. for example,

Employee myMethod(Employee obj)

{

statements;

returned obj;

}

Even the objects are also passed to methods by value.This means, when we send an object to a method,its bit by bit copy will be sent to the method.

How are objects are passed to methods in Java?

Ans: primitive data types ,objects ,even object references---every thing is passed to methods using 'pass by value' or 'call by value' concept.This means their bit by bit copy is passed to the methods.

5 views0 comments

Recent Posts

See All
bottom of page