yes we can call a static method with object
No, the reason is simple: A method marked as a static means, that you don't need create an instance (create an object with the 'new' operator) in order to use it. The main method is the entry point for a java application, therefor there is nothing after you call it. no one who can create an object of the type of your class and call the main method. So the jvm must call the main method with no object reference.
When we call non static method with respect to class object then this pointer is created which keep the reference of that object.
Static java method is the same as a static variable. They belong to a class and not an object of that class. If a method needs to be in a class, but not tied to an object, then one uses static java.
No. Why? By definition. A static method is, precisely, a method that is not meant to operate on an object. It can only work with static fields, and other static methods, of its class.
Static keyword when used with a method, specifies that this method belongs to the class and not a particular instance of the class (a.k.a object of the class) Ex: public class StaticTest { public static String getAuthorName() { return "Anand"; } } Here getAuthorName is the static method and it can be accessed without instantiating an object of the class StaticTest. You can access this method as: String authorName = StaticTest.getAuthorName();
No, the reason is simple: A method marked as a static means, that you don't need create an instance (create an object with the 'new' operator) in order to use it. The main method is the entry point for a java application, therefor there is nothing after you call it. no one who can create an object of the type of your class and call the main method. So the jvm must call the main method with no object reference.
Yes, it is possible to call a static method from a non-static method. However, it is not possible to call a non-static method from a static method without first having an instance to operate on.
When we call non static method with respect to class object then this pointer is created which keep the reference of that object.
Static is a keyword which is used to call the methods or variables without creation of that class object. It will directly calls the respective method and varialbles.
If you need to access a method without creating an object of corresponding class, it need to be a static method.
Yes, we can access all methods which declare with static means then we can access.. ex: class s{ static method() { System.out.println("Welcome"); } } class fun{ public static void main(String args[]) { method(); } }
Static java method is the same as a static variable. They belong to a class and not an object of that class. If a method needs to be in a class, but not tied to an object, then one uses static java.
The main method is static because the JVM would be invoking this method as the starting point of execution. If this is like other normal methods, the JVM would have to instantiate an object of the class before it can call it. This is not possible because this is the starting point. If the main method is static the JVM can directly call this by specifying the class name.
It is declared static because the JVM would need to execute the main method and if it is not static the JVM would need an object of the class to access the method. How can the JVM get an object of a class without invoking it. since it is static, the JVM can easily access it without this need to have an object of the class.
No
No. Why? By definition. A static method is, precisely, a method that is not meant to operate on an object. It can only work with static fields, and other static methods, of its class.
You can call a static method via an object; the other alternative is to use the class name. A common example is the following, to calculate the exponential function: Math.exp(x) Here, exp() is a static function in the class Math. Note that the class name is used instead of an object.