This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes  Static imports Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " Static imports" Watch " Static imports" New topic
Author

Static imports

Ntuthuko Khwezi Makukule
Greenhorn

Joined: May 11, 2014
Posts: 5

When importing static members of a class. Why are they only accessible within the constructor of the calling class, and not outside of it?
Here's the source code to help understand my question.



Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18743
    
  40

Ntuthuko Khwezi Makukule wrote:When importing static members of a class. Why are they only accessible within the constructor of the calling class, and not outside of it?


Please explain what you mean by "not outside of it".

Thanks,
Henry
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38392
    
  23
Welcome to the Ranch

I have added code tags, which you should always use, and doesn't your post look better
No, you can use static imports in any parts of the class. Apart from a public non‑final field being very poor design, this works:-By the way: package names should be lower‑case throughout.
Ntuthuko Khwezi Makukule
Greenhorn

Joined: May 11, 2014
Posts: 5

Henry Wong wrote:
Please explain what you mean by "not outside of it".


if the static members were accessed outside the constructor.
Ntuthuko Khwezi Makukule
Greenhorn

Joined: May 11, 2014
Posts: 5

Campbell Ritchie
Thanks for the help
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18743
    
  40

Ntuthuko Khwezi Makukule wrote:
Henry Wong wrote:
Please explain what you mean by "not outside of it".


if the static members were accessed outside the constructor.


Again, you need to explain what that means. For example, I can do this...




This won't compile... but the reason has nothing to do with the static import, because this...



Won't compile either.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static imports