• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static imports

 
Ntuthuko Khwezi Makukule
Greenhorn
Posts: 5
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21489
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50168
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie
Thanks for the help
 
Henry Wong
author
Marshal
Pie
Posts: 21489
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic