X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Methods in Java :

A small piece of program inside the class is called "method". A method is a function which bind with either class or object. A method define behaviour of object. In OOPs, any operation performed on object is represented as method. An object communicate with another object by using method. Any executable statement must be written inside method.

Example :

class Example { static int x,y,z; static void assign() { x = 10; y = 20; } static void add() { z = x + y; } static void display() { System.out.println(z); } public static void main(String [ ] args) { assign(); add(); display(); } }

Output :

30

Methods are of two types :

  • Instance method




  • Class method




Instance method :

Non - static method of class is called instance method. This method bind with object name. This method can't called without creating object. Java doesn't allow to write method without return type. If method doesn't returned any value it should be defined with void. Instance method performs object operations.

Syntax : [modifier] return_type method_name(parameter) { statements; }

Example :

class Account { private int accno; private String name; private float balance; void setAccount() { accno = 101; name = "John"; balance = 5000f; } void displayAccount() { System.out.println(accno); System.out.println(name); System.out.println(balance); } } class Bank { public static void main(String [ ] args) { Account acc1, acc2; acc1 = new Account(); acc2 = new Account(); acc1.setAccount(); acc2.setAccount(); acc1.displayAccount(); acc2.displayAccount(); } }

Output :

// save as : Bank.java // compile as : javac Bank.java // run as : java Bank 101 John 5000.0 101 John 5000.0

A method within class perform two types of operations :

  • Setter operation




  • Getter operation




Setter operation :

An operation which change the state (values) of the object is called setter method/modified method.


Getter operation :

An operation which doesn't change the state of object is called getter method. This perform accessing operation.


Method with parameters :

Method having parameter receives values and perform operation on object. One method communicate with another method by passing values.

Java supports two ways of calling method :-

  • Pass by value or Call by value




  • Pass by reference or Call by reference




X

Formal parameters :

The term parameter (sometimes called formal parameter) is often used to refer to the variable as found in the function definition.

X

Actual parameters :

Argument (sometimes called actual parameter) refers to the actual input passed.

Pass by value or Call by value :

A duplicate copy of the actual parameter is passed to the formal parameter that's why change made on formal parameter will not have any effect on the actual parameter.


Pass by reference or Call by reference :

The address of the actual parameter is passed to the formal parameter that's why change made on formal parameter will effect the actual parameter.

Example :

class Account { private int accno; private String name; private float balance; void setAccount(int a, String n, float b) { accno = a; name = n; balance = b; } void displayAccount() { System.out.println(accno); System.out.println(name); System.out.println(balance); } } class Bank { public static void main(String [ ] args) { Account acc1, acc2; acc1 = new Account(); acc2 = new Account(); System.out.println(acc1); System.out.println(acc2); acc1.setAccount(101,"John",5000f); acc2.setAccount(102,"Jack",6000f); acc1.displayAccount(); acc2.displayAccount(); } }

Output :

// save as : Bank.java // compile as : javac Bank.java // run as : java Bank Account@4b7c8f7f Account@5eb10190 101 John 5000.0 102 Jack 6000.0

Methods are stored in stack area.

JVM calls the method by sending hashcode of object on which it perform operation or on which it is currently bind(linking method call with method body is called binding).

X

hashcode :

It refers to a way of getting a number from an object so it can be stored in a Hashtable. In Java a hash code can be any integer, and each object type is responsible for generating its own.

Example :

class Product { private int pid; private String pname; private float price; void setName(int p, String pn) { pid = p; pname = pn; } void setPrice(float p) { price = p; } void display() { System.out.println(pid); System.out.println(pname); System.out.println(price); } } class Shopping { public static void main(String [ ] args) { Product p1 = new Product(); Product p2 = new Product(); p1.setName(101,"John"); p1.setPrice(5000f); p1.display(); p2.setName(102,"Jack"); p2.setPrice(6000f); p2.display(); } }

Output :

// Save as : Shopping.java // compile as : javac Shopping.java // run as : java Shopping 101 John 5000.0 102 Jack 6000.0

Immutable Object :

An object whose state (value) cannot be changed after creating with initial value is called immutable object. This class doesn't provide setter methods.


Mutable Object :

An object whose state (value) can be changed after creating with initial value is called mutable object.

Example1 :

class A { private int x; private int y; } class B { public static void main(String [ ] args) { A obj1 = new A(); obj1.x = 100; obj1.y = 200; } }

// The above example is of immutable object because the initial values cannot change. // No setter method is required in immutable object.

Example2 :

class A { int x; int y; void setXY() { x = 100; y = 100; } } class B { public static void main(String [ ] args) { A obj2 = new A(); obj2.x = 100; obj2.y = 200; obj2.setXY(); } }

// The above example is of mutable object. Setter method must be present.

Methods with return type :

A method having return type as void can't return any value. A method performs operation on object and return values. A method can have return type as :-

  • Primitive type - It returns value.




  • Reference type - It returns object.




Every method should have a method stack area so that it must create their local variable.

Example 1 :

class Account { private int accno; private float balance; void setAccount(int a, float b) { accno = a; balance = b; } float getBalance() { return balance; } } class ATM { public static void main(String [ ] args) { Account acc1; acc1 = new Account(); acc1.setAccount(101,5000f); float bal = acc1.getBalance(); System.out.println("Balance " + bal); } }

Output :

// save as : ATM.java // compile as : javac ATM.java // run as : java ATM Balance 5000.0

Example 2 :

class Book { private String bname; private float price; void setBook(String bn, float p) { this.bname = bn; this.price = p; } String getBook() { return bname + " " + price; } } class BookLibrary { public static void main(String [ ] args) { Book bk1; bk1 = new Book(); bk1.setBook("Java",5000f); String b = bk1.getBook(); System.out.println(b); } }

Output :

// save as : BookLibrary.java // compile as : javac BookLibrary.java // run as : java BookLibrary Java 5000.0

this reference :

For every non - static method of class, Java compiler add a reference variable "this". "this" is a keyword which stores/holds an hashcode of current object on which method perform operations. It is a constant variable whose value is never changes. If instance and load variable are declared with same, instance variable of current object are refer using "this" reference. "this" reference can be implicitly call i.e. it should be call by compiler implicitly and "this" is a local variable. Instance method always called by sending the hashcode of object i.e. stored in "this" pointer.

X

implicitly :

It refers to a way that is not directly expressed.



X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.