#what does static function differ from normal function?

1 messages · Page 1 of 1 (latest)

mild starBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
Hi

A static function in Java belongs to the class itself, rather than to instances of the class. This means that you can call a static function without creating an object of the class. Static functions are commonly used for utility methods or for operations that do not require access to instance-specific data.

On the other hand, a normal function in Java belongs to instances of the class and can only be called on an object of that class. Normal functions are used to perform operations specific to each instance of the class and can access instance variables and other instance methods.

In summary, the main differences between static and normal functions in Java are:

  • Static functions belong to the class itself, while normal functions belong to instances of the class.
  • Static functions can be called without creating an object of the class, while normal functions can only be called on objects of the class.
  • Static functions are typically used for utility methods or operations that do not require access to instance-specific data, while normal functions are used for operations specific to each instance of the class.
sudden hare
#

Java is an object oriented language. In Java we pass messages to objects by calling their methods.

To pass a message to an object you call a non-static method via a reference to that object.

Static methods don't belong to objects and aren't used to pass messages to objects. You can think of them more as functions independent of an object. They are still declared on a class, but they're just functions of that class, not messages to pass to objects of that class. You call a static method via a class, not a reference to an object.

The same is true of fields. When you declare a non-static field, every object of that class has its own version of that field. When you declare a static field, there's only one, and you access it via the class.

#

There is another difference between static and non-static methods.

A non-static method can be overridden. A subclass can provide a more specific implementation for their object definition. So when you call a non-static method, Java must find the appropriate version of the method to call at runtime. This is unnecessary for static methods, because static methods are not affected by inheritance and not overridden - the compiler knows exactly which version it will call at compiletime.

mild starBOT
#

The keyword static controls ownership (not accessibility). A method or field that is static belongs to the class instead of an instance of a class. Suppose there is a simple class called Person:

class Person {
  String name;

  Person(String name) {
    this.name = name;
  }

  String getName() {
    return name;
  }
}

Now, you can create as many instances of this class as you want:

Person first = new Person("John");
Person second = new Person("Jane");

And each person has his own name, because it is non-static. So we can do

System.out.println(second.getName()); // prints Jane

In total, since there exist 2 instances of the Person class now, we also have 2 names and also 2 getName() methods. Each belonging to its corresponding instance of the class.

Now, if something is static, it belongs to the class and not to instances of it. While we just created 2 instances of Person, there exists exactly one class Person itself. So if we write:

class Person {
  static int amountOfEyes = 2;

  String name;

  Person(String name) {
    this.name = name;
  }

  String getName() {
    return name;
  }
}

Then amountOfEyes belongs to the Person class, not to the instances we created. As a result, there exist in total 2 names, 2 getName() methods but only one amountOfEyes variable.

As such, we also access this variable through the class, not through instances of it:

// Like this
System.out.println(Person.amountOfEyes);

So neither first nor second nor third have their own amountOfEyes. There is only one amountOfEyes and it belongs to the Person class itself.

To summarize, static things belong to the class, there is only one class no matter how many instances you create of it. Non-static things belong to instances of a class, each instance has its own set of non-static fields and methods.