The Artima Developer Community
Sponsored Link

Java Buzz Forum
Methods and Type of methods

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Methods and Type of methods Posted: Feb 12, 2015 10:23 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Methods and Type of methods
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement

Method:

  • Method is a sub block of a class that contains logic of that class.
  • logic must be placed inside a method, not directly at class level, if we place logic at class level compiler throws an error.
  • So class level we are allowed to place variables and methods.
  • The logical statements such as method calls, calculations and printing related statements must be placed inside method, because these statements are considered as logic.

  1. package com.instanceofjava;
  2. class sample{
  3.  
  4. int a;
  5. int b;
  6.  
  7. System.out.println("instance of java"); // compiler throws an error.
  8.  
  9. }
  10.  

  1. package com.instanceofjava;
  2. class sample{
  3.  
  4. static int a=10;
  5.    
  6. public static void main(String args[]){

  7. System.out.println(a); // works fine,prints a value:10
  8.  
  9.  }
  10. }

Method Terminology:

1.Method Prototype:

  • The head portion of the method is called method prototype.
  1. package com.instanceofjava;
  2. class sample{
  3.  
  4. static int b=10;
  5.    
  6. public static void main(String args[]) // ->method prototype.
  7. {

  8. System.out.println(b); 
  9.  
  10.  }

2.Method body and logic:

  • The "{ }" region is called method body, and the statements placed inside method body is called logic.

  1. package com.instanceofjava;
  2. class sample{

  3. public static void main(String args[]) // ->method prototype.
  4.  
  5.   int a=10,b=20; // method logic
  6.   int c=a+b;  // method logic
  7.   System.out.println(c);  // method logic
  8.  
  9.  }
  10. }

3.Method parameters and arguments:

  • The variables declared in method parenthesis "( )" are called parameters.
  • We can define method with 0 to n number of parameters.
  • The values passing to those parameters are called arguments.
  • In method invocation we must pass arguments according to the parameters order and type.

  1. package com.instanceofjava;
  2. class sample{

  3. public void add(int a, int b) // ->method prototype. int a, int b are parameters
  4.  
  5.   int c=a+b;  // method logic
  6.   System.out.println(c);  // method logic
  7.  
  8.  }
  9.  
  10. public static void main(String args[]) // ->method prototype.
  11.  
  12.   sample obj= new sample();
  13.   
  14.  obj.add(1,2); //method call , here 1, 2 are arguments.

  15. }
  16. }

4:Method signature:

  • The combination of method "name + parameters "  is called method signature

  1. package com.instanceofjava;
  2. class sample{

  3. public void add(int a, int b) // ->method prototype. int a, int b are parameters
  4.  
  5.   int c=a+b;  // method logic
  6.   System.out.println(c);  // method logic
  7.  
  8.  }
  9.  
  10. public static void main(String args[]) // ->method prototype.
  11.  
  12.   sample obj= new sample();
  13.   
  14.  obj.add(1,2); //method call , here 1, 2 are arguments.

  15. }
  16. }

  • In the above program add(int a, int b) and  main(String args[]) are method signatures.

5. Method return type:

  • The keyword that is placed before method name is called method return type.
  • It tells to compiler and JVM about the type of the value is returned from this method after its execution
  • If nothing is return by method then we can use "void" keyword which specifies method returns nothing.

  1. package com.instanceofjava;
  2. class sample{

  3. public int add(int a, int b) // ->method prototype. int a, int b are parameters
  4.  
  5.   int c=a+b;  // method logic
  6.   return c;
  7.  }
  8.  
  9. public static void main(String args[]) // ->method prototype.
  10.  
  11.   sample obj= new sample();
  12.   
  13. int x= obj.add(1,2); //method call , here 1, 2 are arguments.
  14. System.out.println(x);

  15. }
  16. }

Read: Methods and Type of methods

Topic: Create Thread without extending Thread and implementing Runnable Previous Topic   Next Topic Topic: Node.js gets a new master

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use