Você está na página 1de 2

Static methods cannot be inherited in the sub class because they belong to the class in which they have

been
declared and not to the sub class.

Why java doesn’t support static constructor?

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.

Static Constructor Alternative – Static Blocks

Java has static blocks which can be treated as static constructor.

Usage of a static block

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.

We can have static constructor by marking their class final.

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.

Do static functions have a “this” pointer?

 No, they do not. Because a static function can be called without using an object, the “this” pointer is
not passed into the function.

 “this” pointer is only used when a class is instantiated as an object.


 this and super cannot be used in static context.

Static Methods

The problem with static methods is that


 they are not class methods, but global functions prefixed with a classname
 it is strange that they are "inherited" to subclasses
 it is surprising that they cannot be overridden but hidden
 it is totally broken that they can be called with an instance as receiver

therefore you should


 always call them with their class as receiver
 always call them with the declaring class only as receiver
 always make them (or the declaring class) final

and you should


 never call them with an instance as receiver
 never call them with a subclass of their declaring class as receiver
 never redefine them in subclasses

public class MainClass {


public static void main(String[] args) {
Bar.test();
Foo.helloWorld();
Bar.helloWorld();
}
}

class Foo {

public static void helloWorld() {


System.out.println("Hello world! in foo ");
}
}

class Bar extends Foo {

public static void helloWorld() {


System.out.println("Hello World! in bar ");
}

public static void test() {


System.out.println(" in bar test ");
}
}
output
in bar test
Hello world! in foo
Hello World! in bar

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

Você também pode gostar