• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

static imports

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source: Whizlabs, but a bit modified for study

I have two files: test.java in package InnerClass and another file called test1.java.
Code for both is this:





If Line 2 in test1.java is commented, I get the error "cannot find symbol x".

I understand that we have to do a static import of x because x is declared static in test.java.

But my doubt is: Even the class Utilities is static in test.java. How come I am able to compile test1.java only with import InnerClass.test.*; ?

I mean, without doing a STATIC import of the static CLASS, I am able to use it. But, the same does not apply to the static VARIABLE x...Any differences?
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static import is needed to import only static methods and fields. import package.class.* is enough to import inner classes...
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

import package.class.* is enough to import inner classes...



So, even for static inner classes, just a normal import package.class.* is enough is it?
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually if you know there are no inner classes in java. So static inner class and non-static inner class are basically the same (they have some differences but it is not relevant here). So import package.class.* syntax is enough to import all the inner classes i.e. static and non-static...
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok Ankit..
But your quote " .........there are no inner classes in java. " what does it mean?
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well if you are really interested in knowing that, you can read my blog. the link is in my signature...
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok fine .. Thanks for the replies
 
and POOF! You're gone! But look, this tiny ad is still here:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic