Static imports in 1.5

same static variable and same static method exists in two diffrent classes, while importing these two in my own implementation class follwing different output is coming why? Using j2se version1.5
while using variable imports like
import static package.Class1.variable1;
import static package.Class2.variable1;
//giving compilation errros
while using method imports like
import static package.Class1.method1;
import static package.Class2.method1;
//it is not giving any compilation error.
Can anyone explain?

>
ah ok, when i wrote "Clearly" above i was assuming
that the spec. when it does arrive (or when i can find
it..) will disallow static imports of same signature
methods (or possibly even just same named methods?) -
and clearly any contravention of the spec should be
reported to the userIf the spec says is should do something then it should do that.
>
on the other hand, the more the compiler can do the
better since its a single point used by lots of
people, so any small extra only has to be done once
and is multiplied in the payoff - any individual
trivial error message or warning might be
questionable, but if lots are done then it saves time
I doubt that a lot of people are going to be attempting to apply static imports to the same named item.
not completely the same things, but i like the jikes
feature where it says something like "it look like
you're trying to invoke <SOME API METHOD> - this
doesn't exist but a similar method does with
signature: ... "
I suspect that happens and is going to continue to happen far more frequently than duplicate static imports.
And of course a compiler can add additional things like that. But that is rather different than suggesting that it is required.
when a case is left in the grey area then at the very
least a dozen developers will come across it and
because of the lack of some text saying ERROR will
start to question themselves - i don't see this as
pandering to the developers laziness
And yet none of the compilers warn against the "i = i++" construct.
i've not verified this, and also the lang. spec does
not seem to have been updated for 1.5???
it sounds like the compiler should just complain in
both cases?Yes but given that neither has been used I suspect
that it will complain once they are used. If it
doesn't then that is a bug.hm, a similar piece of code
import java.util.List; import java.awt.List;
complains immediately if you try to compile it - i guess this
isn't that important but i like the
complain-loudly-and-early approach - i think they also
possibly made unused imports or duplicate imports
errors or warnings for 1.5?Could be. However, the spec itself drags significantly behind the additional material. For example note that the 'current' spec doesn't have assert. So tracking down what should really be in the spec can be somewhat complicated.
Note also that I haven't really seen the OP clarify that they expect an error for both cases rather than that they expect that no error at all should show up (because they really want to use both.) And although the first is nice the second is clearly wrong. Hopefully that point is clear.

Similar Messages

  • Static import from SE5 (Ex 16 on p. 91 in Thinking in Java Edn 4 by Eckel)

    The following line of code is underlined:
    import static net.mindview.util.Print.*;with this error displayed:
    static import declarations are not supported in -source 1.4
    (try -source 1.5 to enable static import declarations).
    My Path variable contains these:
    C:\Program Files\Java\jdk1.6.0\bin;C:\Program Files\Java\jre1.6.0\bin;D:\java\TIJ4\code
    D:\java\TIJ4\code is the root folder for net.mindview.util.Print.
    JRE1.6 and JDK1.6 are installed.
    I also have:
    i.e. a dot, in my CLASSPATH variable.
    This is from the 4th edn of Thinking in Java by Bruce Eckel. If you are or have been through this stuff I would really appreciate it if we could meet on Skype every now and then to talk about issues like the one described above.
    Thanks for your help,
    b

    Now same code is underlined:
    import static net.mindview.util.Print.*;but with this error message:
    package net.mindview.util does not exist
    However it is mapped like this:
    D:\java\TIJ4\code\net\mindview etc..
    So, I have put this in to the Path variable:
    D:\java\TIJ4\code because it is the root folder of net.mindview.util.
    Is there something else I should do? Is is ok to have a non C directory in the Path variable?
    Thanks for any help,
    B

  • Cannot static import in JSP (WebLogic 10.3)

    I am facing a problem with the use of 'static import' in JSP. This is on WebLogic 10.3. Please help/advise.
    Running weblogic.jspc results in the following error:+
    $ java weblogic.jspc -verboseJspc -keepgenerated -d /tmp/jsp WEB-INF/jspf/fragment.jspf
    \[jspc\] Overriding descriptor option 'keepgenerated' with value specified on command-line 'true'
    \[jspc\] Compiling /WEB-INF/jspf/fragment.jspf
    jspc failed with errors :weblogic.servlet.jsp.CompilationException: fragment.jspf:6:17: Syntax error on token "static", Identifier expected after this token
    <%@page import="static java.util.Collections.EMPTY_LIST" %>
    ^-------------------------------------^
    fragment.jspf:6:17: The import java.util.Collections.EMPTY_LIST cannot be resolved
    <%@page import="static java.util.Collections.EMPTY_LIST" %>
    ^-------------------------------------^
    The generated Java file is not correct; the static import is split into multiple imports+
    $ head /tmp/jsp/jsp_servlet/_web_45_inf/_jspf/__fragment_jspf.java
    package jsp_servlet._web_45_inf._jspf;
    import java.lang.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.jsp.*;
    import static;
    import java.util.Collections.EMPTY_LIST;
    public final class __fragment_jspf extends weblogic.servlet.jsp.JspBase implements weblogic.servlet.jsp.StaleIndicator {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

    I'm not sure that JSPC supports static imports, I would open a support case to get clarification.

  • BUG: JDeveloper code editor shows valid static import call as error

    I've noticed what appears to be a bug in the JDeveloper code validator when dealing with static imports.
    The following classes demonstrate the problem:
    package test;
    public class Parent {
      public static void parentMethod() {
        System.out.println("called parentMethod.");
    package test;
    public class Child extends Parent {
      public static void childMethod() {
        System.out.println("called childMethod.");
    package test;
    import static test.Child.*;
    public class StaticInheritanceTest {
      public static void main(String[] args) {
        Child.childMethod();
        Child.parentMethod();
        childMethod();
        parentMethod();  //<-- this line is highlighted as an error
    }Notice how on lines 7 and 8 of StaticInheritanceTest, the code validator does recognise that both parentMethod and childMethod are static methods of the Child class.
    Since 'Child' has been statically imported, then parentMethod should be available. Although parentMethod is presented as an 'auto-suggest' at line 11 in StaticInheritanceTest, when you try to call it the code editor shows the call as an error and the Ctrl-Click (to drill down into the method) doesn't work.
    When I run the class, it compiles successfully and runs with the following output:
    called childMethod.
    called parentMethod.
    called childMethod.
    called parentMethod.This might seem like an obscure and theoretical problem, but I've hit this whilst using Mockito, which uses exactly this kind of inherited static method to provide matchers.

    Hi,
    thanks. Will file a bug. This seems to be a designtime only issue that does not present a show stopper.
    Frank

  • Static import of member of class in same unnamed package fails

    I think I found a bug in the JDK 1.5 (Tiger) javac compiler. It involves static import and unnamed packages.
    Consider the following compilation unit:
    import static D.x;
    class C {
        int foo() { return x; }
    class D {
        static final int x = 1;
    }Running javac from the Tiger beta 2 SDK generates the following output:
    Test.java:1: cannot find symbol
    symbol: class D
    import static D.x;
                  ^
    Test.java:1: static import only from classes and interfaces
    import static D.x;
    ^
    Test.java:4: cannot find symbol
    symbol  : variable x
    location: class C
        int foo() { return x; }
                           ^
    3 errorsIn my opinion, this is a bug. This compilation unit should compile without errors.
    Note that the following compilation unit compiles without errors with Tiger beta 2:
    package p;
    import static p.D.x;
    class C {
        int foo() { return x; }
    class D {
        static final int x = 1;
    }Note that this issue is distinct from the issue of importing a class from an unnamed package into a named package. I totally agree that the latter is not allowed. However, the current issue is a different matter, since here I am importing members from the same package (which happens to be an unnamed package).

    The unnamed package feature is deprecated. Even if this is a bug (which I sort of doubt), this may never be fixed.

  • Using static import

    I just downloaded the 2.2ea. I tried using the static import feature. When I try to import my own classes f.ex.
    import static test1.test2.TestInferface;
    i get the error
    cannot find symbol
    symbol : class test2
    location: package test1
    import static test1.test2.TestInferface;
    ^
    if I try the static import from the example
    import static java.lang.Math.*; // import static
    it compiles fine. What have I got wrong here?

    Doh, you were right :). Importing test1.test2.TestInterface.*; is what is needed. Works now. Thanks.
    Thought I had already tried that but I guess I was wrong. I guess I was too stuck in the old way of thinking of importing classes and interfaces as a whole, I guess the static import imports class and interface members then.. Am I right?

  • Static Import question

    It is a very easy (down to earth) question.
    Are static imports of methods having the same name as
    Object methods forbidden?public class StaticUse1 { 
      public static boolean equals(StaticUse1 su1,StaticUse1 su2) {
        return true;
      public static boolean equal(StaticUse1 su1,StaticUse1 su2) {
        return false;
    }An import somewhere else:import static apackage.StaticUse1.*;
    public class Test {
      public static void main(String[] args) {
        //System.out.println(equals(new StaticUse1(),new StaticUse1()));  // error
        System.out.println(equal(new StaticUse1(),new StaticUse1()));
    }Compiler complains about ... equals(java.lang.Object) ...

    Such static imports are allowed, but they won't work because of the scoping rules of the language. The equals(Object) for the current class always shadow such methods from the static import.

  • Static import bug?

    Hey just discovered something new...does this sound like a bug to you guys?
    I have a class with a static method called, say, omgMethod()
    and I have another class with a method (not static) with the same name (omgMethod) but different signature.
    now when I try to use import static classA.omgMethod in the second class, it generates a compiler error:
    cannot find symbol method omgMethod( etc...)
    any thoughts? i'm thinking about adding it to the bug report.
    -Cuppo

    I'm not so sure about that. I have never used static import so I checked out Sun's tutorial which is here:
    http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html
    It talks about importing "members" and says you can import "members" individually. And (I had to look this up too in the JLS), "members" means classes, interfaces, fields, and methods.
    So that says to me that you can indeed import a static method from another class, and then use it without qualification. Yeah, I'm surprised too. But you're just importing the method's name, not its signature. So if that name conflicts with a name of a method in your class, then I don't know what is supposed to happen. I looked through the JLS a bit and section 6.5.7.1 says
    "The Identifier must name at least one visible (�6.3.1) method that is in scope at the point where the Identifier appear or a method imported by a single-static-import declaration (�7.5.3) or static-import-on-demand declaration (�7.5.4) within the compilation unit within which the Identifier appears.
    See �15.12 for further discussion of the interpretation of simple method names in method invocation expressions."
    Possibly section 15.12 discusses conflict resolution in this case. But I will let you carry on from there.

  • "static import" construct?

    Hello,
    in a recent Java Live session (http://developer.java.sun.com/developer/community/chat/JavaLive/2002/jl0212.html), respected Joshua Bloch speaks about a "static import construct" that should help clarifying the way constants are defined and used. According to the context, I assume it would be a facility enabling to "import somepackage.someclass.someFinalField;" so as to use "someFinalField" as a shortcut.
    I found nothing published about this construct on Sun's site (bug DB, fora, articles,...).
    Do you have a link about this potential future language feature?
    And is its purpose wider than constants shortcuts (not worth the effort, I deem)?
    Jerome

    Since no-one replied, I'll answer my own question and assign all the cyber-dollars to myself ;-)
    Hello,
    in a recent Java Live session
    (http://developer.java.sun.com/developer/community/chat
    JavaLive/2002/jl0212.html), respected Joshua Bloch
    speaks about a "static import construct" that should
    help clarifying the way constants are defined and
    used. According to the context, I assume it would be a
    facility enabling to "import
    somepackage.someclass.someFinalField;" so as to use
    "someFinalField" as a shortcut.
    I found nothing published about this construct (...).
    Do you have a link about this potential future
    language feature?This topic has been publicez recently as part of JSR-201, which answers my question.
    See http://jcp.org/aboutJava/communityprocess/jsr/tiger/static-import.html for reference.
    Jerome

  • Static import in J2se5

    Hi All,
    I m using jdk1.5. in which we hav a new feature - "static import".
    I want to know, whether this is possible in JSP......?????
    If anybody knows... please give me the ans with the syntax.
    Thanks,
    Chandrakanth.

    I'm usin tomcat 5.0, configured with jdk1.5.
    In servlets i m usin static import n it works fine......
    I dont know... how put the static import statement in JSP file....
    Can v put static import using page directive....s????????

  • Alpha compuler cant compile enums or static imports?!?

    Pardon me if this is the wrong place to ask this question but I couldnt find any forums specific to CAP program members:
    When trying to compile enums and static imports that work with the public prototype compiler, I get the following errors:
    C:\dev\hcj\tiger\src>c:\j2sdk1.5.0\bin\javac -version oreilly/hcj/tiger/*.java
    javac 1.5.0-beta
    oreilly/hcj/tiger/EnumAttach.java:18: 'class' or 'interface' expected
    public abstract enum EnumAttach {
                    ^
    oreilly/hcj/tiger/EnumAttach.java:35: 'class' or 'interface' expected
    ^
    oreilly/hcj/tiger/ErrorLevel.java:20: 'class' or 'interface' expected
    public enum ErrorLevel {
           ^
    oreilly/hcj/tiger/ErrorLevel.java:31: 'class' or 'interface' expected
    ^
    oreilly/hcj/tiger/ForEach.java:49: ';' expected
                    for (String person: importantPeople) {
                                      ^
    oreilly/hcj/tiger/ForEach.java:55: illegal start of expression
            ^
    oreilly/hcj/tiger/ForEach.java:81: ';' expected
                    for (String person: people) {
                                      ^
    oreilly/hcj/tiger/ForEach.java:87: illegal start of expression
            ^
    oreilly/hcj/tiger/StaticImports.java:15: <identifier> expected
    import static StatusColors.*;
           ^
    oreilly/hcj/tiger/StaticImports.java:15: '.' expected
    import static StatusColors.*;
                                ^
    oreilly/hcj/tiger/StaticImports.java:16: <identifier> expected
    import static java.lang.Math.*.
           ^
    oreilly/hcj/tiger/StaticImports.java:21: '.' expected
    public class StaticImports {
           ^
    12 errorsIs there something I am doing wrong or is this known bug?

    Use the compiler flag -source 1.5

  • Static Imports

    What is the advantage of static imports? .........

    They allow you to refer to static members without having to fully qualify them. As you'll see, they should be used sparingly. Read more here:
    [1] http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html
    [2] http://www.deitel.com/articles/java_tutorials/20060211/
    People on the forum help others voluntarily, it's not their job.
    Help them help you.
    Learn how to ask questions first: http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch
    ----------------------------------------------------------------

  • Static import in XQuery

    Hi,
    I am writing a xquery to grab some of the element values and using schema functions like resolve-QName inside the XQuery in Oracle Service Bus. But when i want to import the schema, i had to give the full URI as below.
    iimport schema namespace xs="http://www.w3.org/2001/XMLSchema" at "http://www.w3.org/2001/XMLSchema.xsd";
    For this to work, i have to configure the proxy settings of my network for weblogic so that it can connect to w3 and import the schema. Is there a way to give the schema as static reference? I have already created a schema file in OSB by downloading the XMLSchema.xsd from w3.
    Cheers,
    Kuppusamy.V.,

    try using
    declare namespace xs = "http://www.w3.org/2001/XMLSchema";
    instead of import. It should then resolve for the standard xs schema

  • Static import

    import static java.lang.System.*;
    out.println("Hello");
    System.out.println("Hello");Is there any difference in performance between the above code.

    No
    Kaj

  • Why is this static import of an Enum not working?

    I get the error:
    State.java:3: cannot find symbol
    symbol: class Command
    import static Command.*;
    ^
    /** FIRST CLASS **/
    import static Command.*;
    public enum State
         Authorization( new Command[] { USER, PASS, APOP, QUIT } ),
         Transaction( new Command[] { STAT, LIST, RETR, DELE, NOOP, RSET, UIDL, TOP, QUIT } ),
         Update( new Command[] { QUIT } );
         private Command[] allowedCommands;
         State(Command[] allowedCommands)
              this.allowedCommands = allowedCommands;
         //Rest is elided...
    /** SECOND CLASS **/
    public enum Command
         USER, PASS, APOP, QUIT, STAT, LIST, RETR, DELE, NOOP, RSET, TOP, UIDL;
    }

    DrClap wrote:
    You forgot to include the name of the package which the Command enum is in. Your import statement should look like this:
    import static package.Command.*where "package" is the name of the package.Yes, I was just testing something with the enums before putting it back into my classes with packages. So the test had no packages. When I retried it in my classes (with packages) it worked. Thanks!
    If Java (i.e. the JVM) is going to have restrictions on things working without packages, why allow not using them at all? Shouldn't it just require packages instead of undocumented and inconsistent restrictions?

Maybe you are looking for

  • Where can i buy a hard drive caddy and connector for a second hard drive for my dv7-7190eo?

    Where can i buy a hard drive caddy and connector for a second hard drive for my dv7-7190eo? This question was solved. View Solution.

  • Oracle Business Rules Custom Functions

    Hi, I have an input fact with attributes A,B,C & D and an output fact with E, C & D. (C & D attributes are common in both facts) Whenever the rule matches, we are asserting new output fact by assigning a constant value to attribute 'E' and also the t

  • Sporadic problems connecting to Windows Network

    I'm one of two Mac persons on a Windows network here at work. The problem is that we often can't connect to the server/network. It's a Windows 2003 Server, and we're both running on Tiger. What puzzles me is that we sometimes can log on to the networ

  • My DVD player is not reading DVDs

    I have a Mac Pro running OS 10.6.8, and a Sony DVD RW DW-D150A disc drive. My problem is that when I load DVD's into the DVD drive, sometimes I get a dialogue box that tells me that I've loaded a blank disc, even when I know I haven't.  The drive doe

  • TS1559 Wi-Fi grayed down button

    Hello Apple..I have something to ask you. Why does my iPhone Wi-Fi button grayed down..And you know what all of my contact numbers were gone already as I went to the Apple Store and restore my iPhone..All of the numbers were very important for me and