TestingDocs.com
    Software Testing website
    • Automation
      • Selenium
      • JBehave Framework
    • Tutorials
      • MySQL Tutorials
      • Testlink
      • Maven
      • Git
    • IDEs
      • IntelliJ IDEA
      • Eclipse
    • Flowcharts
      • Flowgorithm
      • Raptor
    • About

    Java Tutorials

    Java static methods

    Overview

    Java static methods do not require objects to be created. We can invoke a static method with the class name. These methods are also known as class methods.

    We can use the static keyword to declare a method as a static method. For example, to declare a public static method

    public static return_Type methodName( method_parameters ){
    
    // method statements
    
    }

     

    Examples

    Let’s see an example of static method.

    public class StaticMethodsDemo {
    
     public static void main(String[] args) {
     System.out.println(exp(3,3));
     }
    
     //Static method to compute x^y
     private static double exp(double x,double y) {
     return Math.pow(x, y); // Math class static method
     }
    }

     

    Java static methods

     

    All the methods in the code are static methods.

    Math.pow() method is a static method . It returns the value of the first argument raised to the power of the second argument. Notice that we have invoked the static method with the class name. Math class is defined in the java.lang package. It is not necessary to import this class in the program. All the classes in the java.lang package are implicitly imported by the complier.

     

    <ClassName>.<static_method>

    Example

    We cannot access or refer to non-static variables or call non-static methods in the static methods. We would encounter the following errors:

    Cannot make a static reference to the non-static field

    Cannot make a static reference to the non-static method

    —

    Java Tutorials

    Java Tutorial on this website:

    https://www.testingdocs.com/java-tutorial/

    For more information on Java, visit the official website :

    https://www.oracle.com/java/

     

     

    Related Posts

    Java Performance

    Java Tutorials /

    Improving Java Performance with Multithreading

    Download Greenfoot Windows

    Java Tutorials /

    Download & Install Greenfoot on Windows

    Java Tutorials /

    Java Static Code Analysis

    Java Tutorials /

    Java Testing Tools

    Java Tutorials /

    Handle Multiple Exceptions in Java

    ‹ Java Arithmetic Operators› Java IO Streams

    Recent Posts

    • ChatGPT Subscription Plans
    • Stellar Converter for Database
    • Stellar Log Analyzer for MySQL
    • Stellar Repair for MySQL
    • ChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI Features
    • Shaping the Future of Development: Exploring Key Trends in Software Engineering
    • Improving Java Performance with Multithreading
    • Open-source Vector Databases

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com