File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes what does it mean ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what does it mean ?" Watch "what does it mean ?" New topic
Author

what does it mean ?

Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

can someone please illustrate this line with a example.
you can not make a call to an instance method, or access an instance variable, until after the super constructor runs.


OCPJP 6 86%
Nikhil Padharia
Ranch Hand

Joined: Apr 19, 2012
Posts: 37
It only means that the access to instance variable or method is only available after the instance is created.
As super constructor gets called automatically when you initialize the object with new..

For eg
Here when you initialize
A a = new A();
By default super gets called and before that you cannot access the members
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

Nikhil Sagar wrote:can someone please illustrate this line with a example.
you can not make a call to an instance method, or access an instance variable, until after the super constructor runs.


First, you should always provide complete context. In this case, this statement is talking about the code in the constructor.

Anyway, why don't you try it yourself? .... note.... yes, there is a part of the constructor that can execute code before the super constructor runs. And if you try to use instance variables there, the compiler will complain.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Nikhil Padharia wrote:It only means that the access to instance variable or method is only available after the instance is created.
As super constructor gets called automatically when you initialize the object with new..

For eg
Here when you initialize
A a = new A();
By default super gets called and before that you cannot access the members


So ,in short this implies that super(); must be the first statement in the Constructor.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Nikhil Sagar wrote:
So ,in short this implies that super(); must be the first statement in the Constructor.


Not exactly, but close.

In every single constructor except for Object's, the very first statement is always either a this(...) to invoke some other constructor on that object, or super(...) to invoke some superclass constructor.

If any c'tor does not explicitly have one of these as its first statement, then the compiler will insert a call to the no-arg super() c'tor as the first statement.
Prabaharan Gopalan
Ranch Hand

Joined: Oct 16, 2009
Posts: 66

super(); must be the first statement in the Constructor.


Not always. You can have overloaded constructors and in those cases, it's allowed to call one of the other constructors (like this(x, y)) and not super(). But eventually, the last constructor in the chain must have super() as the first statement.

Please take a look at this: http://www.javabeginner.com/learn-java/java-constructors to get some code samples.

Edit: Dang! Beaten by a minute. I guess I need to type faster or stop searching for code examples to give. (not to mention that I tried to explain the same line as Jeff.. )


Googling doesn't make you a genius. But not Googling makes you dumber.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

S, jeff these two lines have exactly meaning , Right ?.
you can not make a call to an instance method, or access an instance variable, until after the super constructor runs.


In every single constructor except for Object's, the very first statement is always either a this(...) to invoke some other constructor on that object, or super(...) to invoke some superclass constructor.


Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

Nikhil Sagar wrote:S, jeff these two lines have exactly meaning , Right ?.
you can not make a call to an instance method, or access an instance variable, until after the super constructor runs.


In every single constructor except for Object's, the very first statement is always either a this(...) to invoke some other constructor on that object, or super(...) to invoke some superclass constructor.





No.... These are two different facts. One doesn't imply the other.

And as I already metioned, it is possible to try to code to access intstance variables in the constructor *before* the super constructor runs -- and have the compiler complain if you try to do it.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

And since my first post was ignored, I'll just give you the answer....

If you have a super constructor that takes parameters, it is possible to place an expression as the parameter to the super constructor. If you do, and the expression tries to access instance variables or call instance methods, the compiler will complain -- as at the point of processing the parameters, the super constructor has not run yet.

Hope this helps,
Henry
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4461
    
    8

Another case that can give you unexpected behaviour (though not a compiler error) is if you call a method in a constructor, and then override that method in a subclass. For example, what would you expect to be printed when you create a SubClass object in the following case?

Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Henry Wong wrote:And since my first post was ignored, I'll just give you the answer....

If you have a super constructor that takes parameters, it is possible to place an expression as the parameter to the super constructor. If you do, and the expression tries to access instance variables or call instance methods, the compiler will complain -- as at the point of processing the parameters, the super constructor has not run yet.

Hope this helps,
Henry

Sir,
can you Please explain the above lines.....please please please.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Nikhil Sagar wrote:
Henry Wong wrote:And since my first post was ignored, I'll just give you the answer....

If you have a super constructor that takes parameters, it is possible to place an expression as the parameter to the super constructor. If you do, and the expression tries to access instance variables or call instance methods, the compiler will complain -- as at the point of processing the parameters, the super constructor has not run yet.

Hope this helps,
Henry

Sir,
can you Please explain the above lines.....please please please.


What part exactly do you not understand?
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Jeff Verdegan wrote:
Nikhil Sagar wrote:
Henry Wong wrote:And since my first post was ignored, I'll just give you the answer....

If you have a super constructor that takes parameters, it is possible to place an expression as the parameter to the super constructor. If you do, and the expression tries to access instance variables or call instance methods, the compiler will complain -- as at the point of processing the parameters, the super constructor has not run yet.

Hope this helps,
Henry

Sir,
can you Please explain the above lines.....please please please.


What part exactly do you not understand?


Yeah i got it now..
Just For Confirmation i am posting what i understand.
it means we cant call to any instance method or cant access any instance variable before call to super.if we are calling to the parametrised super than there should not be any use of instance members in the passing arguments.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Henry Wong wrote:And since my first post was ignored,


No Sir there is nothing like that. Actually i am a student and in a very initial stage of core java. i did't understand your answer (i know it's my problem if i am unable to understand anything described by the Author of many books.) thats why i didn't replied to you.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Nikhil Sagar wrote:
Jeff Verdegan wrote:
Nikhil Sagar wrote:
Henry Wong wrote:And since my first post was ignored, I'll just give you the answer....

If you have a super constructor that takes parameters, it is possible to place an expression as the parameter to the super constructor. If you do, and the expression tries to access instance variables or call instance methods, the compiler will complain -- as at the point of processing the parameters, the super constructor has not run yet.

Hope this helps,
Henry

Sir,
can you Please explain the above lines.....please please please.


What part exactly do you not understand?


Yeah i got it now..
Just For Confirmation i am posting what i understand.
it means we cant call to any instance method or cant access any instance variable before call to super.if we are calling to the parametrised super than there should not be any use of instance members in the passing arguments.


Any one please confirm it..........
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8195
    
  23

Nikhil Sagar wrote:Yeah i got it now..
Just For Confirmation i am posting what i understand.
it means we cant call to any instance method or cant access any instance variable before call to super.if we are calling to the parametrised super than there should not be any use of instance members in the passing arguments.

Any one please confirm it..........

Confirmed (at least as I understand your statement).

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Matthew Brown wrote:Another case that can give you unexpected behaviour (though not a compiler error) is if you call a method in a constructor, and then override that method in a subclass. For example, what would you expect to be printed when you create a SubClass object in the following case?



Whoa !!! first i was totally confuse. After Looking at the output. (i am a beginner)
At last i conclude that constructordoes following things step by step...
1. it returns the reference id at te first step.
2. call to super();
3.initialize the instance variable (either with the default value or with the user assigned values).
4.after that is there is something to do in the constructor is does.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39788
    
  28
You should not try to guess; you should read the Java Language Specification. It’s not easy reading. You are probably interested in §12.4 at present.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Campbell Ritchie wrote:You should not try to guess; you should read the Java Language Specification. It’s not easy reading. You are probably interested in §12.4 at present.

Thanks again Sheriff. I was looking something like this.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what does it mean ?