Java/J2EE and SOA (325 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (41 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (324 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

What Is Java Static Method?

Last updated on Oct 22,2019 4.1K Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

If you have ever learnt or tried learning Java then you would know Java Static Method is a concept that does create confusion to some extent. In this article I will try to demystify this Java concept. Following pointers will be covered in this article,

So let us get started then,

Java Static Method vs Instance Method

Instance Methods

Methods that require an object of its class to be created before calling it is called as  Instance methods. To invoke a instance method, we have to create an Object of the class in within which it defined.

Sample:

public void Sample(Str name)
{
//Execution Code....
}
// Return type should be something from the following int, float String even user defined data types will do.

Static Methods

Static methods do not depend on the need to create object of a class. You can refer them by the class name itself or meaning you refer object of the class.

Sample:

public static void Example(Str name) 
{ 
// code to be executed.... 
} 
//Ensure To static modifier in their declaration. 
//Return type just like the last example can be int, float, String or user defined data type.

Let us move on to the next topic of this article,

Java 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 member of a class.

Sample

//Java Program to demonstrate the use of a static method.
class Student{
int rollno;
String name;
static String college = "ITS";
//static method to change the value of static variable
static void change(){
college = "BBDIT";
}
//constructor to initialize the variable
Student(int r, String n){
rollno = r;
name = n;
}
//method to display values
void display(){System.out.println(rollno+" "+name+" "+college);}
}
//Test class to create and display the values of object
public class TestStaticMethod{
public static void main(String args[]){
Student.change();//calling change method
//creating objects
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
Student s3 = new Student(333,"Sonoo");
//calling display method
s1.display();
s2.display();
s3.display();
}
}

Output

111 Karan BBDIT

222 Aryan BBDIT

333 Sonoo BBDIT

Let us continue with the next part of this article

Restrictions

There are two main restrictions. They are:

  1. The static method can not use non static data member or call non-static method directly.
  2. this and super cannot be used in static context.

Let us move on to the final bit of this article,

Why is the Java main method static?

It is because the object is not required to call a static method. If it were a non-static method, JVM creates an object first then call main() method that will lead the problem of extra memory allocation.

Thus we have come to an end of this article. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog  and we will get back to you as soon as possible.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.