Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Imports Error: Need help Ranchers.

 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I'm working on java 5 "Static imports" Concept.
I have written 2 programs ,One is a client java file and other is a java file containing static entities as shown below.



Below is my client program where im trying to use static imports to import static entities from Static Test.



I have defined all the entites in my StaticTest.java file as "Static",but why am i still getting this error.???

Well Im using Eclipse IDE for this purpose.
The Path of my files are as below:
MyProject->src->(default package)->under this default package i have the files "StaticTest.java" and "StaticTestClient.java"

I will be highly obliged if you can help me out

--
Deepak Lal
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Deepak ,

Though i hv not tried statciu import , my immediate guess would be that methods should be called with () ,otherwsie treated as identifiers .
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,
I have given the static import declaration correctly itself,
Could you please look into again



Could you please tell me what is wrong in it?

--
Deepak Lal
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I might have read the error incorrectly, but I think the Java Language Specification contains the explanation for that error. You usually omit the () after the name of a method in a "static import" declaration.

By the way: please don't write "hv," but "have." Explanation here.
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Balu,
Eclipse automatically resolved into getStaticObject;it should not be getStaticObject();
There should not be an extra ().im not talking about that
im talking about the below statement.



Campbell , please help me out.

--
Deepak lal
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the link I posted?
 
Mauwries ser
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you need to import it because it's in the same package.
lift one of the two to another package and the static import will work.



good luck
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,
Yes, i have read the link
It says the below for SingleStaticImportDeclaration:


SingleStaticImportDeclaration:
import static TypeName . Identifier;


So my below set of statements is also in accordance with the Java language specification for import statements.



Campbell please help me out.
Can you tell me where i have gone wrong....Please
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you find the bit about compiler error when importing from the unnamed package?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch, Maurice Serier.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Deepak, you are dwelling on the wrong part of the spec. You are trying to import from the default package, which the spec says it is a compile time error to try to import from the unnamed (default) package. So you can not do what you want, you need to move the class you are importing from into a package.


Deepak Lal wrote:Hi Campbell,
Yes, i have read the link
It says the below for SingleStaticImportDeclaration:


SingleStaticImportDeclaration:
import static TypeName . Identifier;


So my below set of statements is also in accordance with the Java language specification for import statements.



Campbell please help me out.
Can you tell me where i have gone wrong....Please
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve and Campbell for pointing out the mistake,
Oops i made a silly mistake...

Thanks All for correcting me..
Static Imports scared "ME" for a while.

Thanks Ranchers
--
Deepak Lal
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic