Static Method & non Static method in Java

Опубликовано: 25 Май 2022
на канале: JavaTwist
6
1

Static Method in Java:
In the static method, the method can only access only static data members and static methods of another class or same class but cannot access non-static methods and variables.
Syntax: public static void test(){}
The static method cannot be overridden because of early binding.
The static method uses compile-time or early binding.

Non Static method in Java:
Every method in java defaults to a non-static method without a static keyword preceding it. non-static methods can access any static method and static variable also, without using the object of the class.
Syntax: public void test(){}
The non-static method uses runtime or dynamic binding.
-The non-static method can be overridden because of runtime binding.

Example Code:
package p1;
class A{
//non-static method
public void Test1() {
System.out.println("This is a non static method");
}

//static method
public static void Test2() {
System.out.println("This is a static method");
}

}

public class B {

public static void main(String[] args) {
A a=new A();
a.Test1();// calls the non static method

A.Test2(); // calls the static method

}
}


На этой странице сайта вы можете посмотреть видео онлайн Static Method & non Static method in Java длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь JavaTwist 25 Май 2022, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 6 раз и оно понравилось 1 зрителям. Приятного просмотра!