Escolar Documentos
Profissional Documentos
Cultura Documentos
been
declared and not to the sub class.
Everything that is marked static belongs to the class only, for example static method cannot be inherited in the
sub class because they belong to the class in which they have been declared.
Since each constructor is being called by its subclass during creation of the object of its subclass, so if you mark
constructor as static the subclass will not be able to access the constructor of its parent class because it is
marked static and thus belong to the class only.
This will violate the whole purpose of inheritance concept and that is reason why a constructor cannot be
static
Constructors are not static coz they violate the basic meaning of static. Static means something which is the
property of a class while constructors are something which is property of an object.
If you want to make something happen before your object has created. Say you want to read a file and then
decide which object you want to create. It happens in real time. In that case it is useful.
Static block is only called only before the first instance of the class is created.
why static method can not use non static data member?
No. Because non-static member variables of a class always belong to an object – meaning that every
object has it’s own personal copy of non-static member variables (also known as instance variables).
static functions have no object to work with since they belong to the class as a whole. Remember that
you can call a static function without an object – and for that exact reason a static function can not call
instance variables.
No, they do not. Because a static function can be called without using an object, the “this” pointer is
not passed into the function.
Static Methods
class Foo {
Overriding static methods is not a syntax error, so compiler allows it. Its just that, overriding will not happen at
all. This is called method hiding