-3

Super is non static variable, so it should be used with some object reference. But here below, the object is not yet created, so on which object super is called ?

class A{}
class B extends A{
B() { super(); } 

If object of B is created in main method.

sanidhya
  • 9
  • 5
  • Constructors have a very specific signature to follow `[visibility] ClassName([Parameters ...])`. Why would you wont to made it static ? – AxelH Nov 22 '16 at 06:50
  • I am unsure what you wanna achieve but static blocks are made to address your requirement. `static { //any code that you need to invoke while class loading, before even any constructors are called }` – Karthikeyan Nov 22 '16 at 06:53
  • I suggest you look at this answer first. http://stackoverflow.com/a/3903551/762395 – root Nov 22 '16 at 06:53
  • class A{} class B extends A{} , now if i create object of B in main , on what object constructor of A is called ? – sanidhya Nov 22 '16 at 07:03
  • Ok, you have problems to understand inheritance with the constructor... the constructor of A will be "called" in the instance of B you have created. – AxelH Nov 22 '16 at 07:07
  • Possible duplicate of [order of constructor calls in multilevel inheritance in java](http://stackoverflow.com/questions/17806342/order-of-constructor-calls-in-multilevel-inheritance-in-java) or [this bad answer but with lots of duplicate](http://stackoverflow.com/questions/23899863/java-why-does-my-class-automatically-inherits-constructor-from-superclass?noredirect=1&lq=1) – AxelH Nov 22 '16 at 07:08

1 Answers1

0

Constructors are not static. This is obvious, since you can use this in the constructor. The object it works on is already allocated, it is just not completely initialized.

Thomas Stets
  • 3,015
  • 4
  • 17
  • 29
  • I don't like the example since `this` is not limited to the constructor but visible from every non-static methods. You are just saying that constructors are not static ;) – AxelH Nov 22 '16 at 06:55
  • i'm not good at explaining, this is what i was asking, class A{} class B extends A{} , now if i create object of B in main , on what object constructor of A is called ? – sanidhya Nov 22 '16 at 07:02