• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor calling static method or variables

 
Vasiq Molvizadah
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm reading SCJP for Java 6...and i'm stuck in this point ..which says

"Only static variables or methods can be accessed as part of the call to the super() or this()."

Any example on this statement ...
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you try calling any instance methods or fields, their values may be undefined at the time of a super() or this() call.
 
Henry Wong
author
Marshal
Pie
Posts: 21423
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its actually a bit more than that, you are not allowed to use an instance variable or call an instance method, on the super() expression... meaning you can't use them to evaluate the parameters for the super() call.

Henry
 
Vasiq Molvizadah
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give me an example or a little program....that would be very helpful....

Thanks for the replies...
 
shoeb sayyed
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you really explain with an example!!!
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Vasiq Molvizadah
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the explanation dude......
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A follow-up question : Are statics okay and others not because of initialization order?
Is the following true? Statics are set up as each class is loaded (top-to-bottom in the
code), starting with the loaded (lowest) class, then its super class and on up toward
Object. Then initialization continues from the most super class on down, in each;
class parameters, then initialization code blocks followed by constructor code.

Jim... ...
 
shoeb sayyed
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This works fine. So what difference does it make from the previous one?
 
Vasiq Molvizadah
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Shoeb :- I didn't get your question........
 
shoeb sayyed
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructor is calling static variable and static methods in the code above?

and the code that is written first shows the same, but i didn't get what the first code is trying to prove.

I am confused with the first code.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shoeb sayyed wrote:I am confused with the first code.


Ahh, *Probably* I got you. to be more specific, your question is what is the difference between



and



I get your question? if yes , first one is instance variable and later one is local variable's value

 
shoeb sayyed
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Thanks Seetharam
that was a nice explanation
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:if yes , first one is instance variable and later one is local variable's value

No, the later one is a String literal. Literal values (String, int, etc) are always allowed.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.I understood rob. thanks
 
John Paterson
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,

Firstly my apologies if it was wrong to have revived a 5 year old thread. I find this thread interesting and the following is my take.
Yes, the SCJP 6 (Kathy Sierra) mentions:
"Only static variables and methods can be accessed as part of the call to super()
or this()." I believe this should be interpreted as "In the event a variable or method is accessed as part of the call to super() or this(), than that variable or method should be static."

In the example given by Rob Spoor, he is indeed making a call to a static method as part of the call to super, meaning the super class constructor is invoked with the string value/literal retuned by the getString() method which is static. In Shoeb Sayyed's example, the call to super() does not involve any method or variable, but uses a string literal/value which matches with the super class constructor argument. This is perfectly fine too. However the static methods and the static variable in Shoeb Sayyed's superclass are not necessary, as in they are not relevant to the requirement that this thread is about. That is because the accessing of static variables/methods is only relevant when as part of the call to super() and not for calls made from the super constructor itself(though there is nothing wrong with doing that). The following modification of Shoeb Sayyed's code, with the static keyword stripped off the said variable and method, compliles just as fine:


So I think the important point is the line "as part of the call to super()"


 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there is nothing wrong about reviving old threads to add something relevant. And that post is relevant.

I am sure that is KS (who incidentally founded this website) wrote “only”, she meant “only”. Let's have a look at the Java® Language Specification, and see how much light it casts on that question.
It says (§8.8.7) that a constructor invocation must be the first statement of the constructor, which means super(...); or super(); or this(...); or this(); If you write one of those invocations then it is explicit and if you don't, then an implicit constructor invocation is created with super();. It also says (§8.8.7.1) that…
An explicit constructor invocation statement in a constructor body may not refer to any instance variables or instance methods or inner classes declared in this class or any superclass, or use this or super in any expression; otherwise, a compile-time error occurs.
That means that when KS wrote “only”, she meant “only”. No “should” about it, but definitely “must”. The JLS is quite definite about that.

It is potentially hazardous to refer to static fields directly or indirectly from a constructor because one does not necessarily know their values at object creation time, but it is permitted.So what do those two print instructions print out?

But it is even worse with instance fields.Now, the field field exists in Dog because I made the mistake of not declaring it as private. So what is going to happen if you run that constructor? Are you going to set field to an undefined value, or even to its default value of null? Are you not going to initialise the object in a consistent state? Think what runtime errors you would get.

You are right, that prohibition only applies to explicit constructor calls. You are permitted to use static members of the class because (as has already been said) they exist before any instances exist.
You may use instance members elsewhere in the constructor, although there are at least two pitfalls:-
  • 1: The fields may not necessarily have been initialised.
  • 2: If you use a polymorphic method, you may get a different version from what you expected.
  • What will the value of i be in an Animal object?
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic