You are on page 1of 4

Static Method

In Java, a static method is a method that belongs to a class rather than an instance of a class.
The method is accessible to every instance of a class, but methods defined in an instance are
only able to be accessed by that object of a class.

A static method is not part of the objects it creates but is part of a class definition. Unlike
instance methods, a static method is referenced by the class name and can be invoked without
creating an object of class.

In simpler terms, they are methods that exist even if no object has been constructed yet and
that do not require an invocation object.

• Static methods have access to class variables (static variables) without using the
class’s object (instance).

Syntax
Math.max()
Math.max(value0)
Math.max(value0, value1)
Math.max(value0, value1, /* … ,*/ valueN)
Copy to Clipboard
Parameters
value1, value2, … , valueN

Zero or more numbers among which the largest value will be selected and returned.

Return value

The largest of the given numbers. If any one or more of the parameters cannot be converted
into a number, NaN is returned. The result is -Infinity if no parameters are provided.

Description

Because max() is a static method of Math, you always use it as Math.max(), rather than as a
method of a Math object you created (Math is not a constructor).

Java Program to Find the Maximum Element in a Matrix using and not using static
methods in java

Input : mat[][] = { {1,3,4,19}, {11,10,12,1}, {7,9,0,4,99} }


Output : 99
program
import java.io.*;
// Main class
class GFG {

// Method 1
// To find the maximum element
static int max(int mat[][])
{
// Declaring and initializing variable to unity
// holding the maximum element value
int max = 0;

// Iterating over matrix


// using nested for loops

// Outer loop for rows


for (int i = 0; i < mat.length; ++i) {

// Inner loop for columns


for (int j = 0; j < mat[0].length; ++j) {

// Storing the maximum element


max = Math.max(mat[i][j], max);
}
}

// Return the maximum element


return max;
}

// Method 2
// Main driver method
public static void main(String[] args)
{
// Custom input 2D matrix
int mat[][] = { { 1, 3, 4, 19 },
{ 11, 10, 12, 1 },
{ 7, 9, 0, 99 } };

// Calling the method 1 to get max element


// and storing that integer element
int max_element = max(mat);

// Printing the maximum element


System.out.println(max_element);
}
}
Or

import java.io.*;
// Main class
public class GFG{

// Method 1
// To find the maximum element
int maxim(int mat[][])
{
// Declaring and initializing variable to unity
// holding the maximum element value
int max = 0;

// Iterating over matrix


// using nested for loops

// Outer loop for rows


for (int i = 0; i < mat.length; ++i) {

// Inner loop for columns


for (int j = 0; j < mat[0].length; ++j) {

// Storing the maximum element


max = Math.max(mat[i][j], max);
}
}

// Return the maximum element


return max;
}

// Method 2
// Main driver method
public static void main(String[] args)
{
// Custom input 2D matrix
int mat[][] = { { 1, 3, 4, 19 },
{ 11, 10, 12, 1 },
{ 7, 9, 0, 99 } };

// Calling the method 1 to get max element


// and storing that integer element
GFG a=new GFG();
int max_element = a.maxim(mat);

// Printing the maximum element


System.out.println(max_element);
}
}

You might also like