Skip to main content
spelling and grammar
Source Link
Cornelius Dol
  • 64.2k
  • 26
  • 142
  • 190

Static methods are treated as global by the JVM, there are not bound to an object instance at all.

It could conceptually be possible if you could call static methods from class objects (like in languages like Smalltalk) but it's not the case in Java.

EDIT

You can overlaodoverload static method, that's ok. But you can not override a static method, because class are no first-class object. You can use reflection to get the class of an object at run-time, but the object that you get does not parallel the class hierarchy.

class MyClass { ... } class MySubClass extends MyClass { ... } MyClass obj1 = new MyClass(); MySubClass obj2 = new MySubClass(); ob2 insteanceofinstanceof MyClass --> true Class clazz1 = obj1.getClass(); Class clazz2 = obj2.getClass(); clazz2 insteanceofinstanceof clazz1 --> false 

You can reflects on themreflect over the classes, but it stops there. You don't invoke a static method by using clazz1.staticMethod(), but using MyClass.staticMethod(). A static method is not bound to an object and there is hence no notion of this nor super in a static method. A static method is a global function. Byfunction; as a consequence there is also no notion of polymorphism and, therefore, method override makeoverriding makes no sense.

But this could be possible if MyClass was an object at run-time on which you invoke a method, as in Smalltalk (or maybe JRuby as one comment suggest, but I know nothing of JRuby).

Oh yeah... one more stuffthing. You can invoke a static method through an object obj1.staticMethod() but that really a syntactic hacksugar for MyClass.staticMethod() and should be avoided. It usually raises a warning in modern IDE. I don't know why they introducedever allowed this shortcut at first.

Static methods are treated as global by the JVM, there are not bound to an object instance at all.

It could conceptually be possible if you could call static methods from class objects (like in languages like Smalltalk) but it's not the case in Java.

EDIT

You can overlaod static method, that's ok. But you can not override a static method, because class are no first-class object. You can use reflection to get the class of an object at run-time, but the object that you get does not parallel the class hierarchy.

class MyClass { ... } class MySubClass extends MyClass { ... } MyClass obj1 = new MyClass(); MySubClass obj2 = new MySubClass(); ob2 insteanceof MyClass --> true Class clazz1 = obj1.getClass(); Class clazz2 = obj2.getClass(); clazz2 insteanceof clazz1 --> false 

You can reflects on them but it stops there. You don't invoke a static method by using clazz1.staticMethod(), but using MyClass.staticMethod(). A static method is not bound to an object and there is hence no notion of this nor super in a static method. A static method is a global function. By consequence there is also no notion of polymorphism and method override make no sense.

But this could be possible if MyClass was an object at run-time on which you invoke a method, as in Smalltalk (or maybe JRuby as one comment suggest, but I know nothing of JRuby).

Oh yeah... one more stuff. You can invoke a static method through an object obj1.staticMethod() but that really a syntactic hack for MyClass.staticMethod() and should be avoided. It usually raises a warning in modern IDE. I don't know why they introduced this shortcut at first.

Static methods are treated as global by the JVM, there are not bound to an object instance at all.

It could conceptually be possible if you could call static methods from class objects (like in languages like Smalltalk) but it's not the case in Java.

EDIT

You can overload static method, that's ok. But you can not override a static method, because class are no first-class object. You can use reflection to get the class of an object at run-time, but the object that you get does not parallel the class hierarchy.

class MyClass { ... } class MySubClass extends MyClass { ... } MyClass obj1 = new MyClass(); MySubClass obj2 = new MySubClass(); ob2 instanceof MyClass --> true Class clazz1 = obj1.getClass(); Class clazz2 = obj2.getClass(); clazz2 instanceof clazz1 --> false 

You can reflect over the classes, but it stops there. You don't invoke a static method by using clazz1.staticMethod(), but using MyClass.staticMethod(). A static method is not bound to an object and there is hence no notion of this nor super in a static method. A static method is a global function; as a consequence there is also no notion of polymorphism and, therefore, method overriding makes no sense.

But this could be possible if MyClass was an object at run-time on which you invoke a method, as in Smalltalk (or maybe JRuby as one comment suggest, but I know nothing of JRuby).

Oh yeah... one more thing. You can invoke a static method through an object obj1.staticMethod() but that really syntactic sugar for MyClass.staticMethod() and should be avoided. It usually raises a warning in modern IDE. I don't know why they ever allowed this shortcut.

Static methods are associated with a class, so the first sentence is misleading if not wrong.
Source Link

Static methods are treated as global by the JVM, there are not bound to a classan object instance at all.

It could conceptually be possible if you could call static methods from class somehow existed as object in the JVMobjects (like in languages like Smalltalk) but it's not the case in Java.

EDIT

You can overlaod static method, that's ok. But you can not override a static method, because class are no first-class object. You can use reflection to get the class of an object at run-time, but the object that you get does not parallel the class hierarchy.

class MyClass { ... } class MySubClass extends MyClass { ... } MyClass obj1 = new MyClass(); MySubClass obj2 = new MySubClass(); ob2 insteanceof MyClass --> true Class clazz1 = obj1.getClass(); Class clazz2 = obj2.getClass(); clazz2 insteanceof clazz1 --> false 

You can reflects on them but it stops there. You don't invoke a static method by using clazz1.staticMethod(), but using MyClass.staticMethod(). A static method is not bound to an object and there is hence no notion of this nor super in a static method. A static method is a global function. By consequence there is also no notion of polymorphism and method override make no sense.

But this could be possible if MyClass was an object at run-time on which you invoke a method, as in Smalltalk (or maybe JRuby as one comment suggest, but I know nothing of JRuby).

Oh yeah... one more stuff. You can invoke a static method through an object obj1.staticMethod() but that really a syntactic hack for MyClass.staticMethod() and should be avoided. It usually raises a warning in modern IDE. I don't know why they introduced this shortcut at first.

Static methods are treated as global by the JVM, there are not bound to a class at all.

It could conceptually be possible if class somehow existed as object in the JVM (like in languages like Smalltalk) but it's not the case in Java.

EDIT

You can overlaod static method, that's ok. But you can not override a static method, because class are no first-class object. You can use reflection to get the class of an object at run-time, but the object that you get does not parallel the class hierarchy.

class MyClass { ... } class MySubClass extends MyClass { ... } MyClass obj1 = new MyClass(); MySubClass obj2 = new MySubClass(); ob2 insteanceof MyClass --> true Class clazz1 = obj1.getClass(); Class clazz2 = obj2.getClass(); clazz2 insteanceof clazz1 --> false 

You can reflects on them but it stops there. You don't invoke a static method by using clazz1.staticMethod(), but using MyClass.staticMethod(). A static method is not bound to an object and there is hence no notion of this nor super in a static method. A static method is a global function. By consequence there is also no notion of polymorphism and method override make no sense.

But this could be possible if MyClass was an object at run-time on which you invoke a method, as in Smalltalk (or maybe JRuby as one comment suggest, but I know nothing of JRuby).

Oh yeah... one more stuff. You can invoke a static method through an object obj1.staticMethod() but that really a syntactic hack for MyClass.staticMethod() and should be avoided. It usually raises a warning in modern IDE. I don't know why they introduced this shortcut at first.

Static methods are treated as global by the JVM, there are not bound to an object instance at all.

It could conceptually be possible if you could call static methods from class objects (like in languages like Smalltalk) but it's not the case in Java.

EDIT

You can overlaod static method, that's ok. But you can not override a static method, because class are no first-class object. You can use reflection to get the class of an object at run-time, but the object that you get does not parallel the class hierarchy.

class MyClass { ... } class MySubClass extends MyClass { ... } MyClass obj1 = new MyClass(); MySubClass obj2 = new MySubClass(); ob2 insteanceof MyClass --> true Class clazz1 = obj1.getClass(); Class clazz2 = obj2.getClass(); clazz2 insteanceof clazz1 --> false 

You can reflects on them but it stops there. You don't invoke a static method by using clazz1.staticMethod(), but using MyClass.staticMethod(). A static method is not bound to an object and there is hence no notion of this nor super in a static method. A static method is a global function. By consequence there is also no notion of polymorphism and method override make no sense.

But this could be possible if MyClass was an object at run-time on which you invoke a method, as in Smalltalk (or maybe JRuby as one comment suggest, but I know nothing of JRuby).

Oh yeah... one more stuff. You can invoke a static method through an object obj1.staticMethod() but that really a syntactic hack for MyClass.staticMethod() and should be avoided. It usually raises a warning in modern IDE. I don't know why they introduced this shortcut at first.

added 1189 characters in body; deleted 4 characters in body; added 286 characters in body
Source Link
ewernli
  • 38.8k
  • 6
  • 95
  • 123

Static methods are treated as global by the JVM, there are not bound to a class at all.

It could conceptually be possible if class somehow existed as object in the JVM (like in languages like Smalltalk) but it's not the case in Java.

EDIT

You can overlaod static method, that's ok. But you can not override a static method, because class are no first-class object. You can use reflection to get the class of an object at run-time, but the object that you get does not parallel the class hierarchy.

class MyClass { ... } class MySubClass extends MyClass { ... } MyClass obj1 = new MyClass(); MySubClass obj2 = new MySubClass(); ob2 insteanceof MyClass --> true Class clazz1 = obj1.getClass(); Class clazz2 = obj2.getClass(); clazz2 insteanceof clazz1 --> false 

You can reflects on them but it stops there. You don't invoke a static method by using clazz1.staticMethod(), but using MyClass.staticMethod(). A static method is not bound to an object and there is hence no notion of this nor super in a static method. A static method is a global function. By consequence there is also no notion of polymorphism and method override make no sense.

But this could be possible if MyClass was an object at run-time on which you invoke a method, as in Smalltalk (or maybe JRuby as one comment suggest, but I know nothing of JRuby).

Oh yeah... one more stuff. You can invoke a static method through an object obj1.staticMethod() but that really a syntactic hack for MyClass.staticMethod() and should be avoided. It usually raises a warning in modern IDE. I don't know why they introduced this shortcut at first.

Static methods are treated as global by the JVM, there are not bound to a class at all.

It could conceptually be possible if class somehow existed as object in the JVM (like in languages like Smalltalk) but it's not the case in Java.

Static methods are treated as global by the JVM, there are not bound to a class at all.

It could conceptually be possible if class somehow existed as object in the JVM (like in languages like Smalltalk) but it's not the case in Java.

EDIT

You can overlaod static method, that's ok. But you can not override a static method, because class are no first-class object. You can use reflection to get the class of an object at run-time, but the object that you get does not parallel the class hierarchy.

class MyClass { ... } class MySubClass extends MyClass { ... } MyClass obj1 = new MyClass(); MySubClass obj2 = new MySubClass(); ob2 insteanceof MyClass --> true Class clazz1 = obj1.getClass(); Class clazz2 = obj2.getClass(); clazz2 insteanceof clazz1 --> false 

You can reflects on them but it stops there. You don't invoke a static method by using clazz1.staticMethod(), but using MyClass.staticMethod(). A static method is not bound to an object and there is hence no notion of this nor super in a static method. A static method is a global function. By consequence there is also no notion of polymorphism and method override make no sense.

But this could be possible if MyClass was an object at run-time on which you invoke a method, as in Smalltalk (or maybe JRuby as one comment suggest, but I know nothing of JRuby).

Oh yeah... one more stuff. You can invoke a static method through an object obj1.staticMethod() but that really a syntactic hack for MyClass.staticMethod() and should be avoided. It usually raises a warning in modern IDE. I don't know why they introduced this shortcut at first.

Post Undeleted by ewernli
Post Deleted by ewernli
Source Link
ewernli
  • 38.8k
  • 6
  • 95
  • 123
Loading