static methods in Java

A static method is any method that has been declared using static keyword  . For example static void sumTotal(int x,int y) { i...

A static method is any method that has been declared using static keyword .For example
static   void sumTotal(int x,int y)
    {
    int total=x+y;
    System.out.println("Total is " +total);
    }


Characteristics of static methods.

  • static method can be invoked without creating class instance.
  • static method has access to static variables and can change their values.
  • static method belongs to the class rather than object .

Questions on static methods

1.Can a non-static method be called inside a static method?

non-static methods cannot be called or invoked inside a static method.
Try compiling below code.

/**
 *
 * @author Eric
 * www.techoverload.net
 */
public class Sum {
  void sumTotal(int a,int b,int c)
    {
    int total=a+b+c;
    System.out.println("Total is " +total);
    }
    
  
    public static void main(String args [])
    {
   
    sumTotal(123,23,67);//referencing non-static method inside a static method.
    
    }
    
}

Output
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - non-static method sumTotal(int,int,int) cannot be referenced from a static context

Then try this 
/**
 *
 * @author Eric
 * www.techoverload.net
 */
public class Sum {
  static void sumTotal(int a,int b,int c)//static method
    {
    int total=a+b+c;
    System.out.println("Total is " +total);
    }
    
  
    public static void main(String args [])
    {
   
    sumTotal(123,23,67);//referencing static method inside a static method.
    
    }
    
}



Output


run:
Total is 213
BUILD SUCCESSFUL (total time: 0 seconds)

COMMENTS

Name

android arrays cnna1 graphics java java control statements java.swing JButton JFrame JLabel JTextField laravel node.js OPP questions swing technology
false
ltr
item
Code WHIZZ: static methods in Java
static methods in Java
Code WHIZZ
https://code-whizz.blogspot.com/2017/10/can-non-static-method-be-called-inside.html
https://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/2017/10/can-non-static-method-be-called-inside.html
true
5534598864497432585
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy