Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we will discuss how we call methods in Java, beginning with static methods. Can anyone tell me what a static method is?
Isn't it a method that belongs to the class itself?
Exactly! Static methods belong to the class and can be called without instantiating the class. For example, you might call a method like this: MyClass.add(5, 10);. So what's the key advantage of static methods?
I think it's about memory efficiency since we don't need to create an object to use them.
That's correct! When you don't need an object to call a method, you save on memory utilization. Now, let’s summarize this concept. Static methods can be invoked directly using the class name without creating an object.
Moving on to non-static methods. Who can explain how these methods are called?
They are called on an instance of the class, right? Like using obj.display()?
Correct! Non-static methods require an object. For example, after creating an object of MyClass, you'd use obj.display();. Can anyone tell me why we would need to use non-static methods?
Maybe to access or modify instance variables specific to that object?
Exactly! Non-static methods often deal with instance variables. Great job! To conclude, non-static methods are tied to object instances, thus offering functionality specific to each object.
Let’s discuss the primary differences between static and non-static methods in a bit more detail. Who wants to share what they know?
Static methods can be called without an object, but non-static ones require an object to be created first.
Right! Also, consider this: static methods can only directly access static variables, while non-static methods can access both static and instance variables. Why do you think this limitation exists?
Because static methods do not belong to any instance, they don’t have access to instance-level data?
Exactly! To put it concisely, use static methods for utilities or helpers, and reserve non-static methods for operations that rely on instance-specific data. Great interaction today!
Static stays, no object displays; non-static needs, an object indeed.
Imagine a librarian (the static method) who can retrieve any book (invoke), while every student (non-static) needs to visit the library (create an object) to check out their individual books.
F.O.C.U.S - For Only Class Usage Static methods.
S.N.I.P - Static Not Instance Pay, to remember static methods don't need objects.
Term
Static Method
Click to flip
Term
Non-Static Method
Click to flip
Method calling is the process of executing a method in Java, either statically or dynamically.
In this section, we explore how methods are invoked in Java programming. It covers both static and non-static methods, their calling processes, and the impact of method definitions and parameters on these calls.
In Java, method calling refers to the process through which a method is executed. This section distinguishes between two primary types of method invocation: static and non-static methods.
Static methods belong to the class rather than any particular instance of the class. They can be called directly using the class name, without needing to create an object. An example is shown below:
int sum = MyClass.add(5, 10);Here, the static method add is called from MyClass.
Non-static methods, on the other hand, are part of an instance of a class and must be invoked on an object of that class. For instance:
MyClass obj = new MyClass();
obj.display();In this example, the method display() is called using the object obj.
The section emphasizes the importance of understanding method calls for effective programming in Java, contributing to code reusability and modular design.