• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Can we import static variables in java?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know we can import Static Methods and final fields but can we also import static variables which are not initialized yet?
 
Sheriff
Posts: 13730
228
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Seems like that's a question you could answer yourself by trying it in an actual program. Have you done that?
 
Marshal
Posts: 65466
249
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch (again).

That would mean using global variables, which is usually bad practice.
 
Anirudh Prayaga
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the welcome and response guys, yes I was able to import the uninitialized static field (not final) by declaring it public.
 
Junilu Lacar
Sheriff
Posts: 13730
228
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anirudh Prayaga wrote:Thanks for the welcome and response guys, yes I was able to import the uninitialized static field (not final) by declaring it public.


Just because you can, doesn't mean you should. Take care when using static imports, especially if it's a public mutable field. As Campbell pointed out, that makes the variable a global mutable value, which is not advisable to have. Prefer encapsulating those kinds of values instead.
 
Why fit in when you were born to stand out? - Seuss. Tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!