GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • About
  • Reference
  • Integrations
  • API
  • Blog
  • Java Syntax Reference
  • Feedback

This view

  • Original page
  • New address

GeezerVison is showing syntaxdb.com — Class Methods in Java - SyntaxDB - Java Syntax Reference

    Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Languages
    • User Program Communication
      • Print
      • User Input
      • Commenting
      • Read from Text File
      • Write to File
      • Import Package
    • Variables
      • Primitive Data Types
      • Type Casting
      • Variable Declaration
      • Constants
      • Arrays
      • HashMaps
      • HashSets
    • Control Flow
      • If Statement
      • Switch Case
      • Ternary Operator
      • For Loop
      • For Each
      • While Loop
      • Do-While Loop
      • Try Catch Block
    • Object Oriented Programming
      • Class Declaration
      • Access Modifiers
      • Constructor
      • Instantiation
      • Class Variables
      • Standard Methods
      • Class Methods
      • Inheritance
      • Interfaces
      • Abstract Methods and Classes
      • Anonymous Class
      • Declaring an Exception
      • Throwing an Exception
    • String Class
      • String Variables
      • String Length
      • String Compare
      • String Trim
      • Split String
      • Substring
      • String Representation
Categories
Language
Java
  • Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Version: SE 8

Categories

  • User Program Communication
    • Print
    • User Input
    • Commenting
    • Read from Text File
    • Write to File
    • Import Package
  • Variables
    • Primitive Data Types
    • Type Casting
    • Variable Declaration
    • Constants
    • Arrays
    • HashMaps
    • HashSets
  • Control Flow
    • If Statement
    • Switch Case
    • Ternary Operator
    • For Loop
    • For Each
    • While Loop
    • Do-While Loop
    • Try Catch Block
  • Object Oriented Programming
    • Class Declaration
    • Access Modifiers
    • Constructor
    • Instantiation
    • Class Variables
    • Standard Methods
    • Class Methods
    • Inheritance
    • Interfaces
    • Abstract Methods and Classes
    • Anonymous Class
    • Declaring an Exception
    • Throwing an Exception
  • String Class
    • String Variables
    • String Length
    • String Compare
    • String Trim
    • Split String
    • Substring
    • String Representation

Object Oriented Programming

Class Methods in Java

Class methods are methods that are called on the class itself, not on a specific object instance. The static modifier ensures implementation is the same across all class instances. Many standard built-in classes in Java (for example, Math) come with static methods (for example, Math.abs(int value)) that are used in many Java programs.


Syntax
public class className {
    modifier static dataType methodName (inputParameters) { //static method
         //block of code to be executed
    }
}

//calling the method, from anywhere
className.methodName(passedParams);

Notes

methodName is defined by the programmer, dataType is the data type of the result from calling the method, and modifier is the access modifier. The 'static' keyword is an additional modifier that makes the method static.

methodParameters contain data used within the method, stored using variables. Parameters are defined as: (dataType parameterName), which is the data type of the input parameter and the name of the parameter as seen within the method. There can be multiple parameters separated by a comma.

Static methods are called without instantiation. This means that static methods can only access other static members of the class (and of course, global and passed in variables), because other members are object members. They, however, can be called on an object, but this is discouraged.


Example
public class Numbers {
    public Numbers() {}
    public static int findMinimum(int number1, int number2) { 
        //3 stored in number1, 5 stored in number2
        int minimum = number2; 
        if (number1 < number2) minimum = number1; 
        return minimum; 
    }
}

//calling method outside of the class
int min = Numbers.findMinimum(3, 5);
//this would NOT work, static methods cannot be accessed with an object
min = numberClass.findMinimum(3, 5);

See Also
Documentation

Static Class Members

Add to Slack

< Standard Methods   |   Inheritance >


You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.