Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
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,
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,
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
There are two main restrictions. They are:
Let us move on to the final bit of this article,
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 Course Online 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 or you can also join our Java Training in Amritsar.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 21st December,2024 21st December SAT&SUN (Weekend Batch) | View Details |
Java Course Online | Class Starts on 1st March,2025 1st March SAT&SUN (Weekend Batch) | View Details |
edureka.co