質問

Below class causes a compile error at line println("Hello World!"); : The method println(String) is undefined for the type StaticImport :

import static java.lang.Math.*;
import static java.lang.System.*;

public class StaticImport {
    public static void main(String[] args) {
        println("Hello World!");
        out.println("Considering a circle with a diameter of 5 cm, it has:");
        out.println("A circumference of " + (PI * 5) + " cm");
        out.println("And an area of " + (PI * pow(2.5,2)) + " sq. cm");
    }
}

Why can the pow method be accessed in java.lang.Math without explicitly importing pow , unlike the println method where the method name 'out' needs to be used ?

役に立ちましたか?

解決

With static import, you can access the direct members of a class without fully qualifying it. Your static import allows you to access out directly, because it's a member of System, and pow directly, because it's a member of Math. But neither Math nor System has the method println; PrintWriter does (the type of out).

Your static import...

import static java.lang.System.*;
//...
println("Hello World!");

... is equivalent to the following code, which we can see wouldn't compile:

System.println("Hello World!");

他のヒント

println is a method belonging to the out member of the System class. Without that qualifier, Java has no idea what println is.

When you do a static import, you can access the members of a class without having to provide the fully-qualified name. So your static import of java.lang.System lets you access everything that System has. This means that you can access out (a PrintWriter instance) which belongs to System. But it does not mean that you can access println directly because that belongs to out. So you have to tell Java how to get to println by going through out first.

Look at it this way. Assume you have the following structure:

MyClass
   |
   +---Something
   |      |
   |      +---- methodOne
   |      |
   |      +---- methodTwo
   |      |
   |      +---- OtherThing
   |                |
   |                +---- otherMethodOne
   |                |
   |                +---- otherMethodTwo
   +---method

If you did a static import with MyClass.*:

  • You have access to method() (Java translates this to MyClass.method())
  • You have access toSomething. (Java translates this to MyClass.Something)
  • You do not have raw access to other stuff under Something. For example, you cannot simply call methodOne() because Java will translate that to MyClass.methodOne(), and there is no method called methodOne() under MyClass. That method belongs to Something, so you can do Something.methodOne(), or Something.OtherThing.otherMethodOne().

If you did a static import with MyClass.Something.*:

  • You have access to methodOne. (Java translates this to MyClass.Something.methodOne)
  • You have access to methodTwo. (Java translates this to MyClass.Something.methodTwo)
  • You have access to OtherThing. (Java translates this to MyClass.Something.OtherThing)
  • You do not have raw access to otherMethodOne or otherMethodTwo; you will need to do OtherThing.otherMethodOne() or OtherThing.otherMethodTwo().
  • You do not have access to method, because Java translates that to MyClass.Something.method, and there is no such method in the Something class.

pow is a static method of Math class so you can access it without creating object of Math class if you have written import java.lang.Math statement at top, on the other hand out is an member of System class of type PrintStream and println() is a method of PrintStream class and it is not static, so you can't access it in a static way as non-static functions can't be called without creating objects. You can also refer to this link for more about PrintStream http://docs.oracle.com/javase/6/docs/api/java/io/PrintStream.html

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top