• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what does it mean ?

 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nikhil Padharia
Ranch Hand
Posts: 37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21016
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
Java Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.. )
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21016
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21016
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10273
60
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic