JAVA : Difference between a static method and an instance method in Java?

preview_player
Показать описание
JAVA : Difference between a static method and an instance method in Java?

SDET Automation Testing Interview Questions & Answers

We will be covering a wide range of topics including QA manual testing, automation testing, Selenium, Java, Jenkins, Cucumber, Maven, and various testing frameworks.

JAVA : Difference between a static method and an instance method in Java?

In Java, a method can be either static or non-static (sometimes called instance methods).

A static method belongs to the class itself, while an instance method belongs to a specific object (instance) of the class.

Here's an example:

public class MyClass {
public static void staticMethod() {
}

public void instanceMethod() {
}
}

// Call static method

// Call instance method
MyClass obj = new MyClass();

In this example, staticMethod is a static method of the MyClass class, while instanceMethod is an instance method.

The staticMethod can be called directly from the class itself, while the instanceMethod must be called on an instance (object) of the class.
Рекомендации по теме
Комментарии
Автор

JAVA : Difference between a static method and an instance method in Java?

In Java, a method can be either static or non-static (sometimes called instance methods).

A static method belongs to the class itself, while an instance method belongs to a specific object (instance) of the class.

Here's an example:

public class MyClass {
public static void staticMethod() {
System.out.println("This is a static method.");
}

public void instanceMethod() {
System.out.println("This is an instance method.");
}
}

// Call static method
MyClass.staticMethod();

// Call instance method
MyClass obj = new MyClass();
obj.instanceMethod();

In this example, staticMethod is a static method of the MyClass class, while instanceMethod is an instance method.

The staticMethod can be called directly from the class itself, while the instanceMethod must be called on an instance (object) of the class.

sdet_automation_testing