File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Static import statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static import statements" Watch "Static import statements" New topic
Author

Static import statements

Tarun Mohandas
Ranch Hand

Joined: Feb 12, 2013
Posts: 33

Could anyone please tell me why the below import statements are illegal?

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Have you read what are static imports, their syntax and significance? Or are you directly attempting to solve questions to clear some exam?


~ Mansukh
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 223

just read out the stuff for static imports from the last chapter of K&B if you have. (as mansukhdeep said) then try to solve an example.
have a look at this also url- Static Import

Static imports are only used for classes and interface and not for any package directly like "import static java.lang"
And Integer is not a static class. So you cant write "import static java.lang.Integer".
It should refer to a static field of Integer class such as "import static import static java.lang.Integer.MAX_VALUE;"

thanks


OCPJP
Tarun Mohandas
Ranch Hand

Joined: Feb 12, 2013
Posts: 33

I am reading the K&B book. I got a little confused the syntax. I read the concept again and again and I got it now. Thanks
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

You are always welcome. Self-learning is actually the best. You will now remember this for sure. Which K&B book are you learning Java from?
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 223

Tarun Mohandas wrote:I got it now. Thanks


your welcome.. i suggest you to use your own class in static imports..that will b helpful. ie "import static myclass.PI"
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 223

Tarun Mohandas wrote:I got it now. Thanks


some naming convention correction.. "import static MyClass.PI"
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39388
    
  28
Ishan Pandya wrote: . . . i suggest you to use your own class in static imports..that will b helpful. ie "import static myclass.PI"
For reasons explained here, that will not work. You must include the package name.
Tarun Mohandas
Ranch Hand

Joined: Feb 12, 2013
Posts: 33

Mansukhdeep Thind wrote:You are always welcome. Self-learning is actually the best. You will now remember this for sure. Which K&B book are you learning Java from?


I am using K&B SCJP 6. I am using a study guide and oracle website to learn the rest of the stuff in Java SE 7 for OCA.

Ishan Pandya wrote:some naming convention correction.. "import static MyClass.PI"


Thank you for the tip and also for the advise on the syntax by Ritchie
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 223

Campbell Ritchie wrote:For reasons explained here, that will not work. You must include the package name.

I think TypeName is required which can be class itself. I don't see written that you must include a package. Is it mentioned ritchie??
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2310
    
  49
I think TypeName is required which can be class itself.

No, it says "The TypeName must be the canonical name (ยง6.7) of a class type, interface type, enum type, or annotation type.". If you look at section 6.7 it explains what a canonical name is.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

I am using K&B SCJP 6. I am using a study guide and oracle website to learn the rest of the stuff in Java SE 7 for OCA.


If you already know Java language and want to prepare yourself for the SCJP, then please go ahead and study from the current book. But if you are new to Java, read "Head First Java".
Tarun Mohandas
Ranch Hand

Joined: Feb 12, 2013
Posts: 33

Mansukhdeep Thind wrote:If you already know Java language and want to prepare yourself for the SCJP, then please go ahead and study from the current book. But if you are new to Java, read "Head First Java".


I know java. I am able to answer most of SCJP questions but sometimes I get stuck in certain concepts. I go through those concepts repeatedly and get it. I took an OCA mock test in some website and scored 87%. Its only some concepts I find it difficult to grasp, and I am working on it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static import statements