Javatpoint Logo
Javatpoint Logo

Difference Between Static and Instance Methods in Java

Java, as an object-oriented programming language, offers different types of methods to perform various tasks. Two important categories of methods in Java are static methods and instance methods. Both serve distinct purposes and have their own set of characteristics. In this section, we will explore the difference between static and instance methods in Java, along with examples to illustrate their usage.

Static Methods

Static methods, also known as class methods, belong to the class itself rather than to any specific instance of the class. They are associated with the class rather than with any particular object. Here are some key points to keep in mind about static methods:

Declaration: Static methods are declared using the "static" keyword in their method signature. For example:

Access: Static methods can be accessed directly using the class name, without the need for creating an instance of the class. For example:

Instance variables: Static methods cannot directly access instance variables of a class. They can only access static variables (class variables) or other static methods.

Usage: Static methods are commonly used for utility functions or operations that do not require any specific instance of a class. For instance, methods to calculate mathematical operations (e.g., square root) or conversion functions (e.g., Celsius to Fahrenheit) can be defined as static methods.

StaticMethodExample.java

Output:

Sum: 15
Square Root: 5.0

In the example above, the MathUtils class contains two static methods, add() and squareRoot(). These methods can be accessed directly using the class name without creating an instance of the MathUtils class.

Instance Methods

Instance methods, also known as non-static methods, are associated with a particular instance of a class. They are invoked on objects (instances) of a class and can access both static and instance variables of the class. Here are some key points about instance methods:

Declaration: Instance methods are declared without the "static" keyword in their method signature. For example:

Access: Instance methods can be accessed by creating an instance of the class and invoking the method on that instance. For example:

Instance variables: Instance methods can directly access both static variables (class variables) and instance variables of a class.

Usage: Instance methods are commonly used to perform operations on specific instances of a class. It can access and modify instance variables, provide behavior unique to each object, and interact with other instance methods.

InstanceMethodExample.java

Output:

Circle 1 - Area: 78.53981633974483, Perimeter: 31.41592653589793
Circle 2 - Area: 28.274333882308138, Perimeter: 18.84955592153876

In the example above, the Circle class contains two instance methods, calculateArea() and calculatePerimeter(). These methods are invoked on specific instances of the Circle class (circle1 and circle2) and calculate the area and perimeter of each circle. the output displays the area and perimeter of two circles. Circle 1 has a radius of 5.0, resulting in an area of approximately 78.54 and a perimeter of approximately 31.42. Circle 2 has a radius of 3.0, resulting in an area of approximately 28.27 and a perimeter of approximately 18.85.

Static methods and instance methods serve different purposes in Java. Static methods belong to the class itself and can be accessed directly using the class name, while instance methods are associated with specific instances of a class and require the creation of objects to access them. Understanding the difference between static and instance methods is crucial for writing efficient and well-structured Java code.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA