0% found this document useful (0 votes)
2 views

Static Method in Java

The document explains static methods in Java, highlighting their characteristics, syntax, and examples. It details how static methods can access static variables but not instance variables, and provides a comparison between static and instance methods. Additionally, it discusses the significance of the main method being static and outlines the restrictions on static methods.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Static Method in Java

The document explains static methods in Java, highlighting their characteristics, syntax, and examples. It details how static methods can access static variables but not instance variables, and provides a comparison between static and instance methods. Additionally, it discusses the significance of the main method being static and outlines the restrictions on static methods.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

ARMY BURN HALL COLLEGE FOR GIRLS, ABBOTTABAD

Static Method in Java With Examples



The static keyword is used to construct methods that will exist regardless of whether or not any
instances of the class are generated. Any method that uses the static keyword is referred to as a
static method.
Features of static method:
 A static method in Java is a method that is part of a class rather than an instance of that
class.
 Every instance of a class has access to the method.
 Static methods have access to class variables (static variables) without using the class’s
object (instance).
 Only static data may be accessed by a static method. It is unable to access data that is not
static (instance variables).
 In both static and non-static methods, static methods can be accessed directly.
Syntax to declare the static method:
Access_modifier static void methodName()
{
// Method body.
}
The name of the class can be used to invoke or access static methods.
Syntax to call a static method:
className.methodName();
Example 1: The static method does not have access to the instance variable
The JVM runs the static method first, followed by the creation of class instances. Because no
objects are accessible when the static method is used. A static method does not have access to
instance variables. As a result, a static method can’t access a class’s instance variable.
// Java program to demonstrate that
// The static method does not have
// access to the instance variable

import java.io.*;

public class GFG {


// static variable
static int a = 40;

// instance variable
int b = 50;

void simpleDisplay()
{
System.out.println(a);
System.out.println(b);
}

// Declaration of a static method.


static void staticDisplay()
{
System.out.println(a);
}

// main method
public static void main(String[] args)
{
GFG obj = new GFG();
obj.simpleDisplay();
// Calling static method.
staticDisplay();
}
}
Output
40

50

40

Example 2: In both static and non-static methods, static methods are directly accessed.
// Java program to demonstrate that
// In both static and non-static methods,
// static methods are directly accessed.

import java.io.*;

public class StaticExample {

static int num = 100;


static String str = "GeeksForGeeks";

// This is Static method


static void display()
{
System.out.println("static number is " + num);
System.out.println("static string is " + str);
}

// non-static method
void nonstatic()
{
// our static method can accessed
// in non static method
display();
}

// main method
public static void main(String args[])
{
StaticExample obj = new StaticExample();

// This is object to call non static method


obj.nonstatic();

// static method can called


// directly without an object
display();
}
}

Output
static number is 100

static string is GeeksForGeeks

static number is 100

static string is GeeksForGeeks

Why use Static Methods?


1. To access and change static variables and other non-object-based static methods.
2. Utility and assist classes frequently employ static methods.
Restrictions in Static Methods:
1. Non-static data members or non-static methods cannot be used by static methods, and static
methods cannot call non-static methods directly.
2. In a static environment, this and super aren’t allowed to be used.

Why is the main method in Java static?


It’s because calling a static method isn’t needed of the object. If it were a non-static method,
JVM would first build an object before calling the main() method, resulting in an extra
memory allocation difficulty.
Difference Between the static method and instance method

Instance Methods Static Methods

It requires an object of the class. It doesn’t require an object of the class.

It can access only the static attribute of a


It can access all attributes of a class.
class.

The methods can be accessed only using The method is only accessed by class
object reference. name.

Syntax: Objref.methodname() Syntax: className.methodname()

It’s an example of pass-by-value It is an example of pass-by-reference


programming. programming.

You might also like