+ 5
What is static method?
class MyClass { void fun(); { System.out.println("Hello World ") ;} public static void main( String[] args) { MyClass.fun();} } Will this program execute.? If not, why?
8 Réponses
+ 10
Information about static methods 💕
https://www.sololearn.com/learn/Java/2159/
+ 6
https://www.geeksforgeeks.org/static-methods-vs-instance-methods-java/
Static methods vs Instance methods in Java - GeeksforGeeks
good luck
+ 6
/*your fun method isnt declared static also you have a semicolon after its parenthisis which wont work.
try the following in the main class*/
static void fun(){
System.out.print("hello world");}
/*you dont need to create an object of class or use class name if the method is declared static in the main class so you can just call this in the main method like so*/
fun();
+ 4
Classes have members, which are either fields or methods. Each member can be either static or non-static / instance bound.
If a member is static, then it means, that you need no instance of the class to access that member (value of the field or execute the method).
For example:
| class Example {
| public static void test(String text) {
| System.out.println(text);
| }
| }
|
You can call the method the following way:
| Example.test("Hello World");
|
So the syntax is:
| <class_name>.<method_name>(<parameters>);
The non-static (instance bound) methods can be called only on an instance/object of the given class, like:
| class Example {
| public void print() {
| System.out.println("Hi there.");
| }
| }
|
You can call it:
| Example e = new Example();
| e.print();
|
It cannot be called by the class name as the static method.
In points:
+ Static method needs no instance of the class instance to be called
+ Non-static methods need an instance of the class to be called
+ Static methods cannot use the "this" keyword
+ Non-static methods can use the "this" keyword
+ 3
Your code won't execute, because your 'fun' method is not static. Methods which are not static can only be called on instances of the class. If you would mark your 'fun' method as static, the code would be fine. You should only use methods without the static modifier, if you use non-static fields or methods from an instance of the class.
Consider the following example:
class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String toString() {
return "Person{" + name + "}";
}
}
You cannot call Person.toString() because what person you want to convert to a string? You make no mention of any person instance. There is no person to get the name of inside the toString() method. But:
Person p = new Person("Cool Guy");
String str = p.toString();
System.out.println(str);
Will print out "Person{Cool Guy}". That is, because you called the toString() method on that specific Person instance stored in variable 'p' and the toString() method used the 'name' field of that person to create the string.
+ 3
example static main
if a main did not declare static we get an error because compiler cannot bind where the starting execution start.
+ 2
Static method is executed once's an execution of the class where it belong's Start.
0
k ы4к