• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static Imports  RSS feed

 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky and Scott Selikoff

OCP
Oracle® Certified Professional
Java® SE 8 Programmer II
STUDY GUIDE

This is the question of this book: Which of the following statements can be inserted to make FootballGame compile? A import my.sports.Football;
B import static my.sports.*;
C import static my.sports.Football;
D import static my.sports.Football.*;
E static import my.sports.*;
F static import my.sports.Football;
G static import my.sports.Football.*;

The correct answer is D. I do not understand why A is not correct, I think import my.sports.Football includes the public static final variable TEAM_SIZE; obviously that is wrong.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With option A, you are importing the class Football - in other words, you tell the Java compiler that if you use the name "Football" in your source file, it really means the class my.sports.Football.

It does not mean that you can use whatever is inside class Football, such as the constant TEAM_SIZE, without qualifying where this name comes from. If you would import my.sports.Football, then the Java compiler doesn't automatically know that with TEAM_SIZE in your class FootballGame you really mean Football.TEAM_SIZE.

More information: Static Import

For all the details and exact meaning of (static) import statements, see Section 7.5 Import Declarations in the Java Language Specification.
 
Mark Spencers
Ranch Hand
Posts: 51
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how static imports work. You specify static keyword and class name and asterisk. For java compiler to know that you want to import all static methods and variables of class name.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!