ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

Methods

Methods

A method is some number of statements written together which can be called at any point by the method’s name.

Defining Methods

  1. public double method_name(double a ,int b)
  2. {
  3. //body
  4. }

public:Modifier
double: Return type
double a ,int b:Parameter list

Example:

  1. public double sum(double a ,int b)
  2. {
  3. double s=a+b;
  4. return s;
  5. }

Calling a Method:

  1. public class ExamSum
  2. {
  3. public static void main(String[] args)
  4. {
  5. double a = 11.8897;
  6. int b = 1;
  7. int c = sum(a, b);
  8. System.out.println(“Sum = " + c);
  9. }
  10. public double sum(double a ,int b)
  11. {
  12. double s=a+b;
  13. return s;
  14. }
  15. }

Output:

Sum=12.8897

The parameters in java can be passed by value or by reference.

Passing parameters by value:
The example stated above for the calling of method is uses pass by value.Passing Parameters by Value means calling a method with a parameter. Through this the argument value is passed to the parameter.

Passing parameter by reference:
A reference to the actual parameter is passed to the method, that’s why it’s called pass by reference.


Method overloading

When a class has two or more methods by same name but different parameters, it is known as method overloading.
Example:

  1. public class ExampleforOverloading
  2. { public static void main(String[] args)
  3. { int a = 10;
  4. int b = 68;
  5. double c = 6.37;
  6. double d = 3.4;
  7. int r1 = maxFunc(a, b);
  8. double r2 = maxFunc(c, d);
  9. System.out.println("Maximum Value = " + r1);
  10. System.out.println("Maximum Value = " + r2); }
  11. public static int maxFunc(int n1, int n2)
  12. { int max;
  13. if (n1 < n2)
  14. max = n2;
  15. else
  16. max = n1;
  17. return max;
  18. }
  19. public static double maxFunc(double n1, double n2)
  20. {
  21. double min;
  22. if (n1 < n2)
  23. max = n2;
  24. else
  25. max = n1;
  26. return max;
  27. }
  28. }

Output:
Maximum Value=68
Maximum Value=6.37

CONSTRUCTORS:

A constructor is a special method that is used to initialize an object.Every class has a constructor,if we don’t explicitly declare a constructor for any java class the compiler builds a default constructor for that class.
The main difference between method and constructor is that a constructor does not have any return type.
1.Default constructor
Example:

  1. class Eg
  2. { public Eg()
  3. {
  4. System.out.println("This is a default constructor"); }
  5. }
  6. 2.Parametrized constructor
  7. Example:
  8. class Eg
  9. { public Eg(int n, String s)
  10. {
  11. System.out.println("This is a parameterized constructor");
  12. }
  13. }
Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!