static method – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Tue, 25 Jan 2022 10:36:49 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg static method – Programmerbay https://programmerbay.com 32 32 How to Call a Method in Java ? https://programmerbay.com/how-to-call-a-method-in-java/ https://programmerbay.com/how-to-call-a-method-in-java/#respond Sun, 05 Dec 2021 04:19:56 +0000 https://programmerbay.com/?p=8365 In this article, we’ll be discussing meaning of method, how to call a method of different types such as instance method, static method, predefined method and more.

What is a Method ?

A method is a block of code that performs a particular task.  It can be invoked anywhere in a program . It increases code readability and reusability.

What is method

When a compiler encounters a method name, it executes its body. After finishing the method execution, the control is sent back to the caller method’s immediate next statement.

How to create a method in java? 

In order to create a method, the below syntax should be considered :

public static void methodName(int x, int y) {
   // body
}
  • public : access modifier
  • static : non-access modifier
  • void : return type
  • methodName : name of the method
  • int x, int y : method arguments or parameters

A method can be divided into two parts : method header and method body, method header is like signature with description which is defined at very beginning, and method body is, where your logic resides.

For example : 

void sum(int a, int b){   // method header
   int result = a + b;                   // method body
  System.out.println("Sum : "+ result);
    
}

How to Call a Method in Java ?

Calling method using object or calling an Instance method

An instance method is a method that requires an object in order to get called. In other words, we need to create an object of a class in which the method resides and then using the object name followed by dot operator, we can call it.

Syntax:

ObjectName.methodName();

Program:

public class InstanceMethodExample {

private void getMessage(){
System.out.println("Hi this is an example of Instance withod ");
}

public static void main(String[] args) {
InstanceMethodExample obj = new InstanceMethodExample();
obj.getMessage();
}
}

Explanation:

In the above code, we defined an instance method to print a simple message. In main(), we first created the object in which the method was defined  and then after we invoked it using “obj” object.

Calling a Static method

A static method is a method that doesn’t depend on the object of a class in which it resides. It uses static keyword before return type while declaration. In Java, main() is a good example of a static method.

Syntax:

ClassName.methodName();

Program:

public class StaticMethodExample {

private static void getMessage(){

System.out.println("Hi this is an example of Static method ");

}

public static void main(String[] args) {
StaticMethodExample.getMessage();
}
}

However, when we are calling it from the same class, we don’t mandatorily to put className.

Explanation:

In the above code, we defined a static method to display a message. In main(), we are directly calling it without using any object.

Calling pre-defined Method in Java

In Java, standard libraries provide helpful predefined methods. In other words, these are prewritten classes consisting of utility methods, for example, the String class offers methods like trim(),toLowerCase(). These classes can be imported and directly used in our program without the need to write them from Scratch.

If a method in the utility class is an instance method then we would call it ObjectName.methodName(). In case of static, ClassName.methodName();

Program:

class PreDefinedMethodExample {


public static void main(String[] args) {
String str = " Called my utility method ";

System.out.println(" Lower Case : "+str.toLowerCase());
}
}

Explanation:

In above code, we are importing String Class and using the method name toLowerCase() which converts a string to lower-case in our code.

Calling an Abstract Method

A method defined with abstract keyword is termed as Abstract method. It comes with only declaration part and the body is provided by the implementing or child class. In this, the concept of inheritance is come into picture.

An abstract method is defined in an abstract classes and another class that extends it would need to implement its abstract method.

Syntax:

abstract returnType methodName();

Program :

public abstract class AbstractClassExample {

abstract void getMessage();
}


class ChildClass extends AbstractClassExample{
@Override
void getMessage() {
System.out.println("Defining Abstract class method");
}

public static void main(String[] args) {
ChildClass obj = new ChildClass();
obj.getMessage();
}
}

 

]]>
https://programmerbay.com/how-to-call-a-method-in-java/feed/ 0
Static methods & Default Methods in Java 8 https://programmerbay.com/static-methods-default-methods-in-java-8/ https://programmerbay.com/static-methods-default-methods-in-java-8/#respond Sun, 14 Jun 2020 15:50:12 +0000 https://programmerbay.com/?p=6433 An interface describes what a class should look like but not describe how, closely related to class but can’t be instantiated. Before Java 8. Java interface can have abstract method (without method definition) only, but Java 8 onwards, two new features were introduced namely Default method and Static method within interface.

Default Method

-A default method allows to add a new methods in existing interface without affecting the implementing classes.

-It uses default modifier

– It must have a complete method definition. Basically, a dummy body.
– It is not compulsory for implementing class to override a default method but one can.

Reason to introduce it

Prior to Java 8, a class that implements an interface mandatorily had to provide implementation to abstract methods of that particular interface.

However, it was not a big deal, but Java developers noticed that if there is an existing interface which has implemented by quite a large number of classes and there a need arises of adding another method. Then it would need to be overridden by other classes. This would break the code if any of the class omit to implement it.

Therefore, default method came in to picture, now one can add any new feature to existing interface without affecting behaviour of other implementing classes.

Problem:

Before adding new Feature:

No error

After adding new Feature:

error 3

 

Solution

For example:

interface MyInterface {
    default void display() {
        System.out.println("This is dummy definition");
    }
}

public class DefaualtMethodExample implements MyInterface {

    public static void main(String[] args) {
        DefaualtMethodExample obj = new DefaualtMethodExample();
        obj.display();
    }
}

Output:

This is dummy definition

Static method

– Java 8 also supports static method within an interface.

-Method body should be provided to the static method at the time of its declaration.

-It can’t be overridden in implementing classes

-It uses static modifier

– By default, static methods cannot be available in implementing classes. Therefore one need to call it by InterfaceName.methodName()

 

For example:

interface MyInterface {
    static void display() {
        System.out.println("This is static definition");
    }
}

public class DefaualtMethodExample implements MyInterface {

    public static void main(String[] args) {
        MyInterface.display();
    }
}

Output:

This is static definition

 

]]>
https://programmerbay.com/static-methods-default-methods-in-java-8/feed/ 0