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 Constructor calling static method or variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
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: 37970
    
  22
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: 18520
    
  40

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: 19651
    
  18



SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 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: 19651
    
  18

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Constructor calling static method or variables
 
Similar Threads
Private static variables in a static class
Private static variables in a static class
Forward Referencing...
static mathods/variable inheritance
final static variables in beans?