aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes In Consturctor, calling  Static method doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "In Consturctor, calling  Static method doubt" Watch "In Consturctor, calling  Static method doubt" New topic
Author

In Consturctor, calling Static method doubt

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 465


Why do we need to mark the method makeRandonName() as static.The K&B book says
That�s because we cannot invoke an instance method until after the super constructor has run.

My question:What they mean by super constructor?Are they mena no arg constructor?

After called this(makeRandomName()) method this(name) will call the overloaded constructor( Objectivw6263(String name)).this point only I got.Please explain the static part for me.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809

Originally posted by Shiva Mohan:

The K&B book says That�s because we cannot invoke an instance method until after the super constructor has run.



Yes K&B is right. You can't call any instance thing before super class constructor runs.


My question:What they mean by super constructor?Are they mena no arg constructor?


In your case no-arg constructor because you are not explicitly calling Super class constructor.


After called this(makeRandomName()) method this(name) will call the overloaded constructor( Objectivw6263(String name)).this point only I got.Please explain the static part for me.


When you create an instance of Objectivw6263 class, then you will get instances of super class as well.

Instance things can only be accessed when all instances are created up the hierarchy.




Since in Objectivw6263() you are calling overloaded version of same class constructor, so still super class constructor is not called. So accessing any instance thing at that point will return compilation error.

Whereas static thing can be accessed because they are not part of instance rather they are part of class.

The whole point is don't allow any instance member access till all instances up the hierarchy gets created and initialised.

Regards


Naseem


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 465
Thanks for the reply Naseem.So ,In constructor this() will always call in static method.Otherwise it will give compile error.




This above program is not giving the output 10.And also can you please explain again how the constructor is working in this case?Does this constructor invoke super() at first.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
What is Test() here.
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 465
oops.I am sorry.Here is the correction.


can you please explain again how the constructor is working in this case?Does this constructor invoke super() at first
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809


Objectivw6263() will call no-arg constructor of Object class i.e., Object().

When you explicitly provide this(..) as a first line in constructor, then compiler will not insert super().

Keep it simple and short...
Compiler will call no-arg constructor if no explicit superclass constructor or overloaded constructor of same class you have called.

e.g.


In above code A() will not call Object(), because you are calling overloaded constructor of same class.
A(String str) will call no-arg constructor of superclass in this case Object class's Object() constructor.

Remember in any constructor, first line will be either this() or super().

Naseem
S Thiyanesh
Ranch Hand

Joined: Mar 19, 2006
Posts: 142
Constructors will have either super() or this() as their first line(except Object).
So when you have this(), there is no possiblity of the super() being run,
so you can call only the static methods.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: In Consturctor, calling Static method doubt
 
Similar Threads
Is it possible to invoke a method before even the object gets created?
doubt in output(constructor related)
compilation error
this() and super() calls
Doubts about my classes