• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Something weird about static imports

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two java source files as below:

Test2.java

Test.java


My problem is I don't understand why Line 1 does not compile, but Line 2 compiles okay. The static import in Test.java does not import the class Test2 itself, but only the static members of Test2. If that is true, why does Line 2 work - Doesn't the java compiler need Test2 to be visible before it could access its instance member data? If you comment out Line 1, you will see that Test.java compiles and executes.
 
Saloon Keeper
Posts: 14266
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need import statements unless you're actually using the name of whatever it is you're using. You're not using the name Test2 in line 2, so you don't need to import it.

Think about it. Import statements are used to tell the compiler where to find the things you're referring to.
 
Sankaranarayanan Viswanathan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But, then this example below causes a compile error. I am not referring to the 'Inner' class explicitly, but yet I receive a compile error:


But, look at this one below which actually compiles and produces an output of "Inner: 10":


Why does this work?
 
Stephan van Hulst
Saloon Keeper
Posts: 14266
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first fails because you're trying to access a field of a private class. Inner is private, so Test knows nothing about it. This has nothing to do with import statements.
 
Ranch Hand
Posts: 53
MyEclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is there in line 2 just a blank place.
 
Stephan van Hulst
Saloon Keeper
Posts: 14266
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the comments in lines 7 and 8 of Test.
 
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic