Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

On static import.

 
s sivaraman
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

first , check out the code that i'm using




error 1
\j\excercise\Test\SampleTwo.java:7: error: cannot find symbol
Sample s = new Sample();
^
symbol: class Sample
location: class SampleTwo
\j\excercise\Test\SampleTwo.java:7: error: cannot find symbol
Sample s = new Sample();
^
symbol: class Sample
location: class SampleTwo


My question here is , Does static import imports the static members alone? Does the user required to do non-static import if they want to use the non-static members of that class in their code?
i mean , i've to do both import as in above code.
 
Henry Wong
author
Marshal
Pie
Posts: 22113
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s sivaraman wrote:
My question here is , Does static import imports the static members alone? Does the user required to do non-static import if they want to use the non-static members of that class in their code?
i mean , i've to do both import as in above code.


Static import is a feature that was added with Java 5, that basically brings in the static components of another class, and have those components be treated as components of the class. This is *not* what you want to do here.

If all you want to do is not have to fully qualify another class, and so that you won't have a compile error, then just use import. In other words, for what you want, don't do both -- just use the regular import. Static import is not something that you do often.

[EDIT. Correction. For what you are doing in line 10, you will need to used static import.]

Henry
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s sivaraman wrote:My question here is , Does static import imports the static members alone? Does the user required to do non-static import if they want to use the non-static members of that class in their code?
i mean , i've to do both import as in above code.

First let us have a look at the purpose of an import statement. As you know each type (class, interface,...) has a name (*), often referred to as the simple name. In your code example the simple names are Sample and SampleTwo. All classes are organized in packages; if you don't use a package statement, your class will be in the default package (its use is discouraged). Now each type has a fully qualified name: it's the combination of the package name and the type's name. So in your code example the fully qualified names are com.pack.Sample and comm.pack2.SampleTwo.
It's perfectly fine to use the fully qualified name throughout your code. Here's the SampleTwo source code file using fully qualified nameAs you can see yourself, using fully qualified names clutters up your code and makes it less readable. Using an import statement you can use the simple name of the type (class) and the code becomes more readable. Here's the updated code snippet

In order to access static members, it is necessary to qualify references with the class they came from (as you can see in the previous code snippet: Sample.functionTwo();). In Java 1.5 static imports were introduced. The static import construct allows unqualified access to static members. Once the static members have been imported, they may be used without qualification. Using a static import in the SampleTwo source code fileHere's a very important remark: although the language feature is called "static imports", the correct syntax is import static (the other way around). So you could say that a static import declaration is analogous to a normal import declaration: where the normal import declaration imports classes from packages, allowing them to be used without package qualification, the static import declaration imports static members from classes, allowing them to be used without class qualification.
A normal import declaration and a static import declaration are responsible for two different things, so both declarations are required in the SampleTwo source code file. So using a static import for a static member of com.coderanch.ocajp.ClassX will only allow to use that static member without class qualification, but does not allow to use ClassX without package qualification. If you want the latter as well, you'll need a normal import statement for com.coderanch.ocajp.ClassX as well.

And finally, you could use both the normal import statement and the static import statement for individual members (as illustrated in the previous code snippet) or en masse (using the * wildcard) as illustrated in this code snippet

Hope it helps!
Kind regards,
Roel

(*) Anonymous inner classes don't have a name (that's why they are called "anonymous" ), but this topic is not on the OCA exams (it's covered on the OCP exams).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic