The main method can be called just like any other method. Just keep in mind that doing so can create infinite loops.
Example:
class MyClass {
static boolean hasEnteredMain = false;
public static void main(String[] args) {
System.out.println("main()");
if(hasEnteredMain) {
return;
}
hasEnteredMain = true;
f();
}
static void f() {
System.out.println("f()");
main(null);
}
}
A static function is a member function that is not associated with any instance of the class; it has no this pointer.
Yes. Any function can be overloaded. However you cannot override a static member function. Only instance members can be overridden.
Always.
A normal function is any function that is not a member of any class. Normal functions that operate upon a class are referred to as non-member functions, however a non-member function can also be a member of another class. Any class may declare any non-member function to be a friend of the class, in which case the function becomes a friend function.A member function is a member of a class and may be declared static or non-static. Non-static member functions have the following 3 properties:Private access to the class members.Scoped to the class.Must be invoked against an object of the class (has a 'this' pointer).Static member functions have the first two properties only while friend functions have the first property only. Non-member functions that are not friends of the class have none of these properties.
Static member functions, member function templates and constructors cannot be virtual.
A public function is scoped to the class in which it is declared. If declared non-static, then it must be invoked against an instance of the class but if declared static then namespace resolution is required to access the function. A non-member function is not scoped to any class but may be scoped to a namespace. If no namespace is specified, then it is scoped to the (unnamed) global namespace. If scoped to any other namespace then namespace resolution is required to access the function.
Cannot be called from another source.
"this" can only be used within the body of a non-static member function of a class and refers to the current instance of that class. Typically, we only refer to "this" instance when a non-static member function returns a reference to the current instance.
Static functions are tied to a class, not to a particular object. A static function can only access static variables because it has no knowledge of member variables.
In Java, a static function is not attached to a particular object, but rather to the whole class.For example, say we have the following class:class Song {String song;void printSong() {System.out.println(song);}static void printStaticSong() {System.out.println("This is a static song. It has no tune.");}}The static function Song.printStaticSong() can be accessed without creating an instance of the Song class (using the "new" keyword)However it cannot access the members of Song (such as song), since it is not an instance of that object.
function of static relay
function of static relay