permaculture playing cards
The moose likes Beginning Java and the fly likes Constructor calling static method or variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor calling static method or variables" Watch "Constructor calling static method or variables" New topic
Author

Constructor calling static method or variables

Vasiq Molvizadah
Ranch Hand

Joined: Dec 24, 2009
Posts: 66
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 ...


Human Knowledge Belongs to the World.
- Vasiq Mz
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43417
    
  32
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
Sheriff

Joined: Sep 28, 2004
Posts: 19936
    
  44

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


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

Joined: Dec 24, 2009
Posts: 66
Can you give me an example or a little program....that would be very helpful....

Thanks for the replies...
shoeb sayyed
Ranch Hand

Joined: Mar 14, 2010
Posts: 48
can you really explain with an example!!!


Thanks,
Shoeb
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 20049
    
  30



SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Vasiq Molvizadah
Ranch Hand

Joined: Dec 24, 2009
Posts: 66
Thanks a lot for the explanation dude......
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
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... ...


BEE MBA PMP SCJP-6
shoeb sayyed
Ranch Hand

Joined: Mar 14, 2010
Posts: 48


This works fine. So what difference does it make from the previous one?
Vasiq Molvizadah
Ranch Hand

Joined: Dec 24, 2009
Posts: 66
@Shoeb :- I didn't get your question........
shoeb sayyed
Ranch Hand

Joined: Mar 14, 2010
Posts: 48
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Mar 14, 2010
Posts: 48
hey Thanks Seetharam
that was a nice explanation
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 20049
    
  30

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

Joined: Jan 28, 2008
Posts: 5575

Yes.I understood rob. thanks
John Paterson
Ranch Hand

Joined: Mar 12, 2012
Posts: 161
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

Joined: Oct 13, 2005
Posts: 43417
    
  32
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?
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Constructor calling static method or variables
     
    It's not a secret anymore!